python
runtime_error
ai_generated
true
RuntimeError: 没有正在运行的事件循环 文件 ".../pytest_asyncio/plugin.py",行 ...,在 pytest_pyfunc_call 中 result = loop.run_until_complete(...)
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
80%修复率
88%置信度
0证据数
2024-05-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
异步测试被收集但未标记 @pytest.mark.asyncio,或者 pytest-asyncio 模式设置为 'strict'(默认)且缺少标记。插件拒绝运行该协程。
English
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.
解决方案
-
95% 成功率
import pytest @pytest.mark.asyncio async def test_fetch(): result = await fetch() assert result == 42 -
92% 成功率
# pyproject.toml [tool.pytest.ini_options] asyncio_mode = "auto" # or pytest.ini # [pytest] # asyncio_mode = auto
无效尝试
常见但无效的做法:
-
80% 失败
pytest-asyncio already manages the loop; calling asyncio.run() inside a coroutine raises 'asyncio.run() cannot be called from a running event loop'.
-
60% 失败
If pytest reads pyproject.toml instead, the config is ignored and strict mode persists.