Open TE-YongweiSun opened 3 weeks ago
Same issue for me, I have the same event_loop
as @TE-YongweiSun in conftest.py
and with pytest-asyncio v.0.21.1
no problem but with the latest version v0.23.6
I have the notorious attached to a different loop
issue.
Logs:
E RuntimeError: Task <Task pending name='Task-6' coro=<_wrap_asyncgen_fixture.<locals>._asyncgen_fixture_wrapper.<locals>.setup() running at /opt/acc-py/venvs/acc-py-venv/lib/python3.11/site-packages/pytest_asyncio/plugin.py:326> cb=[_run_until_complete_cb() at /opt/acc-py/base/2023.06/lib/python3.11/asyncio/base_events.py:180]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop
For somehow the fixture event_loop
is not used anymore.
Starting from pytest-asyncio v0.23, every level of the pytest test collection hierarchy has its own asyncio event loop. By default, tests marked with @pytest.mark.asyncio
are run in a function-scope event loop, i.e. the event loop is refreshed before every test and closed afterwards. A test marked with @pytest.mark.asyncio(scope="module")
is run in an asyncio event loop that is valid for the scope of an entire module, for example.
Unfortunately, pytest-asyncio v0.23 falsely assumes that the caching scope and event loop scope for async fixtures are the same. In your example, this means that foo
is run in a module-scoped loop, whereas tests/test_1.py::test_func
and tests/test_2.py::test_func
are each run in a different, function-scope event loop.
You'll want to adjust your tests to use
@pytest.mark.asyncio(scope="module")
async def test_func(foo):
...
for everything to run in the same event loop.
Is this something that you can do in your codebase (not the example)?
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
:tests/test_1.py
:tests/test_2.py
:[error]
according to the document Decorators:
I add
event_loop
fixture inconftest.py
:But this error is still there.