pytest-fastapi-pattern

Configure deterministic FastAPI pytest integration tests with httpx ASGITransport and respx mocking.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/saintgo7/claude-skills --skill pytest-fastapi-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytest-fastapi-pattern
Source: https://github.com/saintgo7/claude-skills/tree/main/pytest-fastapi-pattern
Command: npx skills add https://github.com/saintgo7/claude-skills --skill pytest-fastapi-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

FastAPI integration tests often fail or give misleading results when sync TestClient usage skips lifespan startup, database state leaks across tests, and upstream HTTP calls to vLLM/OpenAI are not properly mocked.

Core Features & Use Cases

  • In-process async client with real lifespan: Uses httpx AsyncClient with ASGITransport (lifespan enabled) so startup/shutdown code runs exactly like production.
  • Deterministic, isolated DB state: Uses in-memory SQLite with StaticPool plus a per-test create_all/drop_all flow to prevent cross-test contamination.
  • Upstream-safe mocking for LLM gateways: Uses respx to mock upstream HTTP calls (e.g., vLLM/OpenAI) and ensures mocks are actually exercised.
  • Streaming SSE validation: Verifies streaming outputs at the chunk/byte level using async streaming iteration and SSE markers like [DONE].

Quick Start

Ask to generate a FastAPI pytest integration test setup using httpx ASGITransport with lifespan enabled, an autouse-style DB reset fixture, respx upstream mocking, and SSE chunk assertions for streaming endpoints.

Frequently Asked Questions about pytest-fastapi-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test FastAPI lifespan startup events with pytest?

To test FastAPI lifespan startup with pytest, use httpx AsyncClient configured with ASGITransport so that lifespan startup and shutdown events execute exactly like production, unlike sync TestClient which skips them entirely.

Why does my FastAPI SQLite database state leak between pytest tests?

Database state leaks between pytest tests when isolation is absent. Using in-memory SQLite with StaticPool alongside a per-test create_all and drop_all fixture flow ensures deterministic, isolated database state for each individual test.

How do I mock upstream LLM API calls in FastAPI integration tests?

Upstream LLM API calls in FastAPI tests are mocked using respx. It intercepts HTTP requests to gateways like vLLM or OpenAI and uses assert_all_called to guarantee that the defined mocks are actually exercised during the test.

How do I verify SSE streaming output in FastAPI pytest?

SSE streaming output in pytest is verified using async streaming iteration to capture byte-level chunks. This allows asserting specific SSE markers like [DONE] and validating the exact chunk sequence returned by the streaming endpoint.

Does FastAPI TestClient skip lifespan startup code?

FastAPI sync TestClient skips lifespan startup code, leading to misleading integration test results. Using httpx AsyncClient with ASGITransport resolves this by ensuring lifespan startup and shutdown events run properly within the test.

What is the best way to test FastAPI gateway applications forwarding to vLLM?

The best way to test FastAPI gateway applications forwarding to vLLM is using deterministic async pytest patterns. This involves httpx ASGITransport for lifespan, in-memory SQLite isolation, and respx to mock the upstream vLLM HTTP calls.