# Fastapi Test Scaffold And Writer

> Create or update production-grade pytest scaffolding and tests for FastAPI servers (agent server / MCP server). Use when asked to add unit/integration/e2e tests, pytest config, fixtures, or CI-ready test workflows for FastAPI apps.

- Skill: `leemg1476/fastapi-test-scaffold-and-writer` (Agent Skill)
- Install (CLI): `npx skillmds@latest add leemg1476/fastapi-test-scaffold-and-writer`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leemg1476/fastapi-test-scaffold-and-writer/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: leemg1476 (https://skillmd.com/u/leemg1476)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/leemg1476/fastapi-test-scaffold-and-writer

---


# Fastapi Test Scaffold And Writer

## Overview

Generate pytest configuration, fixtures, and example tests for FastAPI apps with unit/integration/e2e layers. Keep tests fast, deterministic, CI-friendly, and offline by default for unit tests.

## Inputs

Accept one JSON object with the schema provided by the user prompt. Treat missing optional fields as disabled. If `endpoints_spec` is empty, still scaffold config + fixtures + placeholder tests.

## Workflow

1) Inspect project root
- Resolve `project_root` to a real path.
- Read existing `pyproject.toml` or `pytest.ini` and avoid clobbering custom settings.
- Detect existing `tests/` structure and merge rather than overwrite.

2) Resolve FastAPI app
- Import via `app_import_path`.
- If import fails, scan common files (`main.py`, `app.py`, `src/main.py`) for a `FastAPI()` instance and report the best guess in comments.

3) Create or update pytest config
- Prefer `pyproject.toml` when `preferences.use_pyproject_toml` is true and no config exists.
- Ensure: `testpaths=tests`, `addopts=-ra -q`, `strict-markers=true`, markers: `unit`, `integration`, `e2e`, `slow`.

4) Create tests layout
- `tests/conftest.py` with shared fixtures.
- `tests/unit/`, `tests/integration/`, `tests/e2e/` with feature-focused files.

5) Build fixtures
- `app` fixture importing `app_import_path`.
- `client` fixture using `httpx.AsyncClient` + `ASGITransport`.
- `auth_headers` fixture (stub) for dependency overrides.
- Monkeypatch fixtures for external calls (HTTP/DB/Redis) using `monkeypatch` or `respx` if installed.
- Optional deterministic fixtures (time/uuid) only if needed by specs.

6) Generate tests per endpoint_spec
- Unit tests: request validation (422), success (200) with external calls mocked, auth failure tests (401/403) via dependency overrides.
- Integration tests: DB roundtrip using `TEST_DATABASE_URL`; Redis set/get TTL if enabled.
- E2E tests: minimal smoke endpoint call using real dependencies; allow external LLM mock if preferences demand it.

7) CI output
- If `ci.provider=github_actions`, generate `.github/workflows/tests.yml` with unit/integration jobs; include service containers when `integration_tests_use_docker` is true.
- Otherwise, create `docs/ci_test_commands.md` with run commands.

## Test Conventions

- Use `pytest-asyncio` for async tests.
- Use `httpx.AsyncClient` with `ASGITransport` (never spawn uvicorn).
- Unit tests must be offline and fast; mock outbound HTTP and DB/cache clients.
- Use `app.dependency_overrides` for auth/rate-limit dependency toggles.
- Keep fixtures small, documented, and deterministic.

## Output Format

Return:
1) A short "plan" section.
2) A diff-like file list with full contents for each created/modified file.
3) A final summary listing created/modified files and pytest commands:
   - `pytest -m unit`
   - `pytest -m integration`
   - `pytest -m e2e`

Do not add extra explanation beyond what is needed to apply changes.

