← ClaudeAtlas

pytest-asynclisted

Async pytest + pytest-asyncio testing: asyncio_mode=auto, SQLite fixtures, httpx.AsyncClient + ASGITransport, unit/integration split, coverage floor, live markers. Use when writing or reviewing async FastAPI/SQLAlchemy tests, or enforcing TDD. Not for production code. Output: offline-green async tests.
hmj1026/dhpk · ★ 2 · Testing & QA · score 68
Install: claude install-skill hmj1026/dhpk
# pytest (async) Test strategy for async Python services. Pairs with the `python` and `fastapi` modules. TDD: write the failing test first (red), implement to green, refactor. ## Configuration - `pytest-asyncio` with `asyncio_mode = "auto"` (in `[tool.pytest.ini_options]`) — `async def test_*` runs without a per-test `@pytest.mark.asyncio` decorator. - `pytest-cov` enforces a coverage floor (ccas: `--cov-fail-under=70`, measured on unit tests). `pytest-timeout` guards hangs. - Layout: `tests/unit/` (fast, isolated) and `tests/integration/` (ASGI client + real query paths). Exclude wiring-only modules (`__main__.py`, app factory) from the *unit* coverage measure; cover them in integration instead. ## Fixtures - **In-memory SQLite** for both unit and integration: create an async engine on `sqlite+aiosqlite:///:memory:`, create tables per test (or per session with a rollback-per-test transaction), yield an `AsyncSession`. Keep it fast and hermetic. - Build object factories/builders for domain models rather than hand-rolling rows in every test. - Mock external services (Gmail, Telegram, LLM) at the boundary — inject fakes via the same `Depends`/Protocol seams the app uses. Never hit the network in unit tests. ## FastAPI integration tests - Drive the app with `httpx.AsyncClient(transport=ASGITransport(app=app), ...)` — no live server needed. Override DB/auth dependencies with `app.dependency_overrides` to inject the in-memory session and a test user. - A