# RuntimeError: TestClient cannot be used with async functions without an async client.

- **ID:** `python/fastapi-test-client-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

使用FastAPI的TestClient测试异步路由时，未使用异步客户端或事件循环。

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **使用TestClient的async模式** (95% success)
   ```
   from fastapi.testclient import TestClient; from main import app; client = TestClient(app); with client: response = client.get('/async-endpoint')
   ```
2. **使用httpx.AsyncClient直接测试** (90% success)
   ```
   from httpx import AsyncClient; async with AsyncClient(app=app, base_url='http://test') as ac: response = await ac.get('/async-endpoint')
   ```

## Dead Ends

- **在同步测试函数中直接调用异步路由** — TestClient默认同步。 (90% fail)
- **使用pytest-asyncio但不配置** — 需要正确标记测试函数。 (70% fail)
