← All notes

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

Sources

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

Permanent ID: iil:note:01866jhb

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.},
}

Markdown

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.

All artifact files (JSON index)

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.