Skip to content
Latchkey

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".

pytest output
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 awaited

Common 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.

Terminal
pip install pytest-asyncio

Set the mode and mark async tests

pyproject.toml
[tool.pytest.ini_options]
asyncio_mode = "auto"
# or per-test:
# @pytest.mark.asyncio
# async def test_fetch(): ...

How to prevent it

  • Install pytest-asyncio and set asyncio_mode in config.
  • Use the managed loop instead of asyncio.get_event_loop().
  • Mark async tests (or use auto mode) so they are not silently skipped.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →