Closed
Description
I just want to use module scope fixture but I get an error:
[environment]
python==3.10.14
pytest==8.2.2
pytest-asyncio==0.23.7
[Directory]
tests/conftest.py
:
import pytest_asyncio
@pytest_asyncio.fixture(scope="module")
async def foo():
yield "a value"
tests/test_1.py
:
import pytest
@pytest.mark.asyncio
async def test_func(foo):
print("test_1 test_func", foo)
tests/test_2.py
:
import pytest
@pytest.mark.asyncio
async def test_func(foo):
print("test_2 test_func", foo)
[error]
# pytest -s tests/
============================= test session starts ==============================
platform linux -- Python 3.10.14, pytest-8.2.2, pluggy-1.5.0
rootdir: /home/sunyw
plugins: asyncio-0.23.7
asyncio: mode=strict
collected 2 items
tests/test_1.py . [ 50%]
tests/test_2.py E [100%]
==================================== ERRORS ====================================
_________________________ ERROR at setup of test_func __________________________
file /home/xxx/tests/test_2.py, line 3
@pytest.mark.asyncio
async def test_func(foo):
print("test_2 test_func", foo)
file /home/xxx/tests/conftest.py, line 13
@pytest_asyncio.fixture(scope="module")
async def foo():
yield "a value"
E fixture 'tests/test_1.py::<event_loop>' not found
> available fixtures: _session_event_loop, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, event_loop, event_loop_policy, foo, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tests/test_2.py::<event_loop>, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, unused_tcp_port, unused_tcp_port_factory, unused_udp_port, unused_udp_port_factory
> use 'pytest --fixtures [testpath]' for help on them.
/home/xxx/tests/conftest.py:13
=========================== short test summary info ============================
ERROR tests/test_2.py::test_func
========================== 1 passed, 1 error in 0.01s ==========================
according to the document Decorators:
All scopes are supported, but if you use a non-function scope you will need to redefine the event_loop fixture to have the same or broader scope. Async fixtures need the event loop, and so must have the same or narrower scope than the event_loop fixture.
I add event_loop
fixture in conftest.py
:
import pytest_asyncio
import asyncio
import pytest
@pytest.fixture(scope="module")
def event_loop(request):
loop = asyncio.get_event_loop_policy().get_event_loop()
yield loop
loop.close()
@pytest_asyncio.fixture(scope="module")
async def foo():
yield "a value"
But this error is still there.