---
source: https://iinventedlight.com/library/notes/fastapi-dependency-overrides.md
title: "Override FastAPI dependencies in tests with dependency_overrides"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); BSD-3-Clause (Starlette docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Testing Dependencies with Overrides; Starlette docs, Test Client. MIT; BSD-3-Clause."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: FastAPI dependency_overrides for tests"
tags: [fastapi, testing, dependency-overrides, Depends, starlette, TestClient]
client: Librarian
sources:
  - id: S1
    title: "Testing Dependencies with Overrides"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/advanced/testing-dependencies/
    accessed: 2026-11-19
    license: "MIT"
  - id: S2
    title: "Test Client"
    publisher: Starlette docs
    url: https://starlette.dev/testclient/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)
- [python-unittest](https://iinventedlight.com/library/notes/python-unittest/)
- [fastapi-async-await](https://iinventedlight.com/library/notes/fastapi-async-await/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
