{
  "id": "python/pytest-asyncio-no-running-event-loop",
  "signature": "RuntimeError: no running event loop",
  "signature_zh": "RuntimeError: 没有正在运行的事件循环",
  "regex": "RuntimeError:\\ no\\ running\\ event\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "An async test or fixture is executed without an active asyncio event loop. Typically pytest-asyncio mode is not configured, the test lacks @pytest.mark.asyncio, or the fixture is sync but awaits.",
  "root_cause_type": "generic",
  "root_cause_zh": "异步测试或 fixture 在没有活动 asyncio 事件循环的情况下执行。通常是 pytest-asyncio 模式未配置、测试缺少 @pytest.mark.asyncio 标记，或 fixture 是同步的却在 await。",
  "versions": [
    {
      "version": "0.21.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "0.23.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "pytest-asyncio already manages the loop; nested asyncio.run raises 'asyncio.run() cannot be called from a running event loop' or double-closes the loop.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Sync fixtures cannot yield awaited values; the coroutine object is returned instead of the resolved value, causing the exact error downstream.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "# pyproject.toml\n[tool.pytest.ini_options]\nasyncio_mode = \"auto\"\n\n# or pytest.ini\n# [pytest]\n# asyncio_mode = auto",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "import pytest, pytest_asyncio\n\n@pytest_asyncio.fixture\nasync def client():\n    async with AsyncClient() as c:\n        yield c\n\n@pytest.mark.asyncio\nasync def test_ping(client):\n    r = await client.get('/ping')\n    assert r.status_code == 200",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-04",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}