AgentSkillsCN

fastapi-test-scaffold-and-writer

为 FastAPI 服务器(代理服务器 / MCP 服务器)创建或更新生产级的 pytest 脚手架与测试用例。当用户被要求为 FastAPI 应用添加单元测试、集成测试、端到端测试,或配置 pytest 配置文件、fixture,以及构建可直接用于 CI 的测试工作流时,可使用此技能。

SKILL.md
--- frontmatter
name: fastapi-test-scaffold-and-writer
description: 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.

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.
  1. 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.
  1. 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.
  1. Create tests layout
  • tests/conftest.py with shared fixtures.
  • tests/unit/, tests/integration/, tests/e2e/ with feature-focused files.
  1. 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.
  1. 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.
  1. 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.