Override FastAPI dependencies in tests with dependency_overrides
Answer: Use dependency_overrides when a test should not run the original dependency, or any of the sub-dependencies it might have. It is a simple dict. Put the original dependency function as the key, and your dependency override as the value. FastAPI will call that override instead of the original dependency. Drive the app with Starlette's TestClient.
When to override
A common case is an external authentication provider. You send a token and it returns an authenticated user. That provider may charge per request, and each call can take extra time compared with a fixed mock user for tests. You probably want to test the external provider once, rather than calling it in every test. Override the dependency that calls that provider, and return a mock user from a custom dependency in those tests.
How to write the test
Import TestClient from fastapi.testclient (or starlette.testclient). Create the client with your FastAPI application.
The test client allows you to make requests against your ASGI application. The TestClient is built on httpx2. Plain httpx is still supported, but deprecated. The test client exposes the same interface as any other httpx2 session.
| raise_server_exceptions | backend | |---|---| | raise_server_exceptions | True | | backend | "asyncio" or "trio" | | follow_redirects | True | | base_url | "http://testserver" |
See also
Claims
- id: C1 claim: dependency_overrides is a dict mapping a dependency to a test replacement. source: S1 evidence: "app.dependency_overrides attribute"
- id: C3 claim: FastAPI calls the override instead of the original dependency. source: S1 evidence: "will call that override instead of the original dependency"
- id: C4 claim: Starlette TestClient sends requests to an ASGI application. source: S2 evidence: "make requests against your ASGI application"
Sources
- [S1] Testing Dependencies with Overrides. FastAPI docs. https://fastapi.tiangolo.com/advanced/testing-dependencies/ . License: MIT. Accessed 2026-11-19.
- [S2] Test Client. Starlette docs. https://starlette.dev/testclient/ . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing Dependencies with Overrides; Starlette docs, Test Client. MIT; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing Dependencies with Overrides; Starlette docs, Test Client. MIT; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Override FastAPI dependencies in tests with dependency_overrides." iinventedlight Library, iil:note:01866jhb, version of 2026-11-19. https://iinventedlight.com/library/notes/fastapi-dependency-overrides/. License: CC-BY-4.0.
@misc{iil_note_01866jhb,
author = {{iinventedlight}},
title = {{Override FastAPI dependencies in tests with dependency\_overrides}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/fastapi-dependency-overrides/},
note = {Permanent ID iil:note:01866jhb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- fastapi-dependency-overrides.table.json (table; CC-BY-4.0)
- fastapi-dependency-overrides.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-dependency-overrides.md.