Agent Skill
2/7/2026

fastapi-testing

This skill should be used when the user asks about "testing FastAPI", "FastAPI tests", "API testing", "test FastAPI endpoints", "mock FastAPI dependencies", "TestClient", "async API tests"

E
eyadsibai
0GitHub Stars
1Views
npx skills add eyadsibai/ltk

SKILL.md

Namefastapi-testing
DescriptionThis skill should be used when the user asks about "testing FastAPI", "FastAPI tests", "API testing", "test FastAPI endpoints", "mock FastAPI dependencies", "TestClient", "async API tests"

name: FastAPI Testing description: This skill should be used when the user asks about "testing FastAPI", "FastAPI tests", "API testing", "test FastAPI endpoints", "mock FastAPI dependencies", "TestClient", "async API tests" version: 1.0.0

FastAPI Testing

Patterns for testing FastAPI applications.

Core Concepts

Test Clients

ClientUse CaseAsync Support
TestClientSync tests (most cases)No
AsyncClient (httpx)Async endpoints, lifespan eventsYes

Key concept: TestClient wraps the ASGI app - no server needed.


Dependency Override Pattern

FastAPI's killer feature for testing: replace any dependency.

How it works:

  1. Define test version of dependency
  2. Add to app.dependency_overrides[original] = test_version
  3. Run tests
  4. Clear overrides after test

Common overrides:

  • Database session → test database or mock
  • Auth/current user → test user or bypassed auth
  • External services → mocked responses

Test Patterns

CRUD Testing Checklist

OperationHappy PathError Cases
CreateReturns 201, correct data400 validation, 409 duplicate
ReadReturns 200, correct data404 not found
UpdateReturns 200, updated data404, 400 validation
DeleteReturns 204404 not found

Validation Testing

Use parametrize for boundary conditions:

  • Valid inputs → expected success
  • Invalid email format → 422
  • Missing required fields → 422
  • Too short/long values → 422

Database Testing Strategies

StrategyProsCons
In-memory SQLiteFast, isolatedDifferent from prod DB
Test databaseRealisticSlower, needs setup
Transactions + rollbackFast, realisticComplex setup
Mocked repositoryFastestLess integration coverage

Key concept: Use function-scoped fixtures to ensure test isolation.


Project Structure

tests/
├── conftest.py       # Shared fixtures (client, db, auth)
├── test_users.py     # Endpoint tests
├── test_auth.py      # Auth-specific tests
└── factories/        # Test data factories

Common Pitfalls

PitfallSolution
Shared state between testsUse function-scoped fixtures
Forgetting to clear overridesUse fixture with cleanup
Testing implementation not behaviorFocus on HTTP responses
Missing async marksAdd @pytest.mark.asyncio
SQLite vs Postgres differencesUse same DB type for important tests

Quick Reference

Test client fixture pattern: Create client, set overrides, yield, clear overrides

Protected endpoint testing: Override get_current_user dependency

File upload testing: Use files={"file": (name, content, mimetype)}

Resources

Skills Info
Original Name:fastapi-testingAuthor:eyadsibai