python runtime_error ai_generated true

RuntimeError: no running event loop File ".../pytest_asyncio/plugin.py", line ..., in pytest_pyfunc_call result = loop.run_until_complete(...)

ID: python/pytest-asyncio-no-running-loop

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-05-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

An async test was collected without being marked with @pytest.mark.asyncio, or pytest-asyncio mode is set to 'strict' (default) and the marker is missing. The plugin refuses to run the coroutine.

generic

中文

异步测试被收集但未标记 @pytest.mark.asyncio,或者 pytest-asyncio 模式设置为 'strict'(默认)且缺少标记。插件拒绝运行该协程。

Workarounds

  1. 95% success
    import pytest
    
    @pytest.mark.asyncio
    async def test_fetch():
        result = await fetch()
        assert result == 42
  2. 92% success
    # pyproject.toml
    [tool.pytest.ini_options]
    asyncio_mode = "auto"
    
    # or pytest.ini
    # [pytest]
    # asyncio_mode = auto

Dead Ends

Common approaches that don't work:

  1. 80% fail

    pytest-asyncio already manages the loop; calling asyncio.run() inside a coroutine raises 'asyncio.run() cannot be called from a running event loop'.

  2. 60% fail

    If pytest reads pyproject.toml instead, the config is ignored and strict mode persists.