Skip to main content
Dependency overrides allow you to replace dependencies during testing without modifying your application code. This is essential for isolating tests and mocking external services.

The Problem: Testing with Real Dependencies

Consider an application that depends on a database:
Testing this endpoint would connect to the real production database, which is:
  • Slow: Real database connections take time
  • Fragile: Tests fail if the database is unavailable
  • Dangerous: Tests might modify production data

Solution: Dependency Overrides

Use app.dependency_overrides to replace dependencies during testing:
The override maps the original dependency function to the replacement function. FastAPI will call the replacement whenever the original is used.

How It Works

FastAPI’s dependency resolution system checks app.dependency_overrides before calling any dependency. The implementation is in fastapi/dependencies/utils.py:629-644:
  1. Dependency resolution begins
  2. For each dependency, check if an override exists
  3. If yes, use the override; if no, use the original
  4. Cache and resolve the dependency normally
Overrides work for dependencies used anywhere: path operation parameters, nested dependencies, and decorator-level dependencies.

Basic Usage Patterns

Overriding a Simple Dependency

Overriding with Different Parameters

The override dependency can have completely different parameters:
This is powerful for authentication testing. Override authentication dependencies to bypass login during tests.

Advanced Override Patterns

Overriding Generator Dependencies

Generator dependencies with yield can be overridden:
You can override a generator dependency with a regular function, and vice versa. FastAPI handles both correctly.

Overriding Nested Dependencies

When dependencies depend on other dependencies, you can override at any level:
Override at the highest level that makes sense for your test. This minimizes the number of overrides needed.

Overriding Router Dependencies

Dependencies in routers are also overridable:

Overriding with Async Dependencies

Async dependencies can override sync dependencies and vice versa:
FastAPI automatically handles async/sync conversions when resolving overrides.

Testing Patterns

Using Pytest Fixtures

Create reusable override fixtures:

Context Manager for Overrides

Create a context manager for temporary overrides:

Testing Multiple Overrides

Always clean up overrides after tests. Use fixtures, context managers, or explicit cleanup to prevent test pollution.

Common Patterns

Mocking External APIs

Testing Authentication

Testing with Stateful Mocks

Best Practices

Always clean up overrides: Use fixtures, context managers, or explicit cleanup to prevent tests from affecting each other.
Override at the right level: Override leaf dependencies for unit tests, higher-level dependencies for integration tests.
Use stateful mocks carefully: When your mock needs state (like a fake database), ensure it’s reset between tests.
Type hints still apply: Even though you’re overriding dependencies, FastAPI still validates and converts parameters based on type hints.
Don’t override in production: dependency_overrides is for testing only. Never use it in production code.

See Also