pytest-asyncio "no current event loop" / "no running event loop" in CI
An async test (or async fixture) ran without an event loop properly provided by pytest-asyncio - because the asyncio mode/marker is not set, or the plugin is missing. Newer Python also deprecated implicitly creating a loop.
What this error means
Async tests fail with RuntimeError: no running event loop or DeprecationWarning: There is no current event loop (an error under -W error). Coroutine tests may also be reported as skipped or "coroutine never awaited".
tests/test_async.py::test_fetch
loop = asyncio.get_event_loop()
RuntimeError: There is no current event loop in thread 'MainThread'.
# or:
PytestUnraisableExceptionWarning: coroutine 'test_fetch' was never awaitedCommon causes
asyncio mode/marker not configured
Without asyncio_mode = "auto" (or an @pytest.mark.asyncio on each test) plus the plugin installed, pytest does not run coroutines under an event loop.
Relying on the implicit loop
Calling asyncio.get_event_loop() with no running loop is deprecated on newer Python and raises under warnings-as-errors. Tests should use the loop the plugin provides.
How to fix it
Install and configure pytest-asyncio
Add the plugin and set auto mode so coroutine tests run under a managed loop.
pip install pytest-asyncioSet the mode and mark async tests
[tool.pytest.ini_options]
asyncio_mode = "auto"
# or per-test:
# @pytest.mark.asyncio
# async def test_fetch(): ...How to prevent it
- Install
pytest-asyncioand setasyncio_modein config. - Use the managed loop instead of
asyncio.get_event_loop(). - Mark async tests (or use auto mode) so they are not silently skipped.