Sphere
Log in Join
☆ 11

pytest-asyncio 0.23 removed event_loop fixture and now async tests fail

Upgraded pytest-asyncio from 0.21 to 0.23 and every async test errors with fixture 'event_loop' not found. I had a custom event_loop fixture in conftest that I use to set up a test database. The changelog mentions deprecating event_loop but I don't understand the replacement. How do I configure the loop now without rewriting every test? I'm on pytest 8.1. The rest of the suite is fine, it's just the tests with async fixtures that break, so I assume it's tied to that one change.

⚑ Report

4 replies

☆ 0
G glitch ·2026-10-05#1

0.23 removed the event_loop fixture. You now use asyncio_default_fixture_loop_scope config and mark tests with @pytest.mark.asyncio(loop_scope='...'). For your DB setup, make it an async fixture with @pytest_asyncio.fixture and let it run in the same loop as the test that requests it.

⚑ Report
☆ 0
S seg ·2026-10-05#2

So I delete the event_loop fixture entirely? What about the code that did loop.run_until_complete(init_db()) inside it? Do I just move that into an async fixture and await it directly? I want to keep the DB setup shared across the module if possible.

⚑ Report
☆ 0
R race ·2026-10-10#3

Rewrite it as an async fixture: @pytest_asyncio.fixture async def db(): await init_db(); yield. Then have tests request db. pytest-asyncio runs it in the same loop by default. The loop_scope setting controls module versus function scope, and asyncio_default_fixture_loop_scope sets the default.

⚑ Report
☆ 0
E eventual ·2026-10-20#4

Read the 0.23 migration section carefully — there's an asyncio_mode config, 'auto' versus 'strict'. If you had plain async def test_ functions without explicit markers, set asyncio_mode = auto in pytest.ini or they'll be silently skipped. That one is easy to miss because nothing fails loudly.

⚑ Report
Log in to reply.