{
  "id": "python/fastapi-test-client-error",
  "signature": "RuntimeError: TestClient cannot be used with async functions without an async client.",
  "signature_zh": "运行时错误: 没有异步客户端时TestClient不能用于异步函数。",
  "regex": "RuntimeError:\\ TestClient\\ cannot\\ be\\ used\\ with\\ async\\ functions\\ without\\ an\\ async\\ client\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "使用FastAPI的TestClient测试异步路由时，未使用异步客户端或事件循环。",
  "root_cause_type": "generic",
  "root_cause_zh": "使用FastAPI的TestClient测试异步路由时，未使用异步客户端或事件循环。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "在同步测试函数中直接调用异步路由",
      "why_fails": "TestClient默认同步。",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "使用pytest-asyncio但不配置",
      "why_fails": "需要正确标记测试函数。",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "使用TestClient的async模式",
      "success_rate": 0.95,
      "how": "from fastapi.testclient import TestClient; from main import app; client = TestClient(app); with client: response = client.get('/async-endpoint')",
      "condition": "",
      "sources": []
    },
    {
      "action": "使用httpx.AsyncClient直接测试",
      "success_rate": 0.9,
      "how": "from httpx import AsyncClient; async with AsyncClient(app=app, base_url='http://test') as ac: response = await ac.get('/async-endpoint')",
      "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-08-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}