← ClaudeAtlas

fastapi-testinglisted

Testing FastAPI apps with pytest-asyncio and httpx.AsyncClient — fixtures, dependency overrides, DB isolation, auth.
jjackkun/claude-harness-hermes · ★ 1 · Testing & QA · score 74
Install: claude install-skill jjackkun/claude-harness-hermes
# FastAPI Testing Async-first testing patterns for FastAPI with pytest-asyncio and httpx. ## When to Activate - Writing tests for FastAPI routes, services, or dependencies - Setting up test fixtures (DB, client, auth) - Integration tests with a real test database - Mocking external services or dependencies ## Stack - `pytest` + `pytest-asyncio` (mode = "auto" recommended) - `httpx.AsyncClient` with `ASGITransport` — **not** TestClient (which is sync) - Separate test DB, cleaned per-test via transactions or truncate ## Baseline Configuration ```toml # pyproject.toml [tool.pytest.ini_options] asyncio_mode = "auto" testpaths = ["tests"] ``` ## Core Fixtures ```python # tests/conftest.py import pytest from httpx import AsyncClient, ASGITransport from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker from app.main import app from app.db.base import Base from app.deps import get_db TEST_DB_URL = "postgresql+asyncpg://test:test@localhost:5432/test_db" @pytest.fixture(scope="session") async def engine(): engine = create_async_engine(TEST_DB_URL, future=True) async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield engine async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await engine.dispose() @pytest.fixture async def db(engine) -> AsyncSession: """Per-test session wrapped in a rollback so nothing persists.""" connection = await engine.c