Check the HTTP contract explicitly
Create a client for the application and send representative requests. Assert the exact status code and relevant response fields, including invalid inputs. A check that accepts any successful response can miss the wrong payload or a response-model change that affects callers even though the endpoint handler still completes.
Separate business-rule tests from endpoint tests. Call a pricing function directly for a large input matrix, then use endpoint checks for request parsing, validation, authorization, and serialization. This keeps failures easier to diagnose and avoids paying application setup cost for every variation of a rule that does not depend on HTTP.
Override dependencies and restore state
Use app.dependency_overrides with the original dependency callable as the key and a replacement callable as the value. Return a controlled user or repository from the replacement. This makes authorization and not-found cases reproducible without requiring real credentials or a shared database whose state changes between runs.
Clear overrides during fixture cleanup so one scenario does not alter the next. If the application depends on startup or shutdown behavior, use TestClient as a context manager to run the lifespan. Document which external dependencies remain real because that boundary determines what a passing endpoint check actually establishes.
Choose sync or async execution deliberately
Ordinary TestClient tests can use normal def test functions and synchronous request calls. Use the documented asynchronous approach when the test itself must await application-side async work directly. Mixing execution styles without the right setup can produce event-loop errors that obscure the behavior the endpoint check was meant to examine.
Keep database fixtures isolated and assert the relevant side effect when persistence is part of the risk. A mocked repository response cannot prove that a real query or transaction works. Add a focused integration check for those claims, while keeping endpoint unit-like cases deterministic and independent of unrelated service availability.
Working sheet
| Case | Control | Assert |
|---|---|---|
| Valid request | Known dependency result | Status and response fields |
| Invalid body | Malformed synthetic input | Validation response |
| Unauthorized user | Auth dependency override | Access denied contract |
| Missing resource | Repository override | Not-found response |
| Real persistence | Isolated test database | Stored side effect |
Questions about test FastAPI endpoints with pytest and dependency overrides
Are TestClient checks pure unit tests?
Usually they exercise routing, validation, and dependency wiring. Describe the actual boundary rather than relying only on the unit label.
Why do dependency overrides leak between tests?
They modify application state. Clear or restore the mapping in fixture cleanup.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
