tezvyn:

How would you override a FastAPI dependency during testing?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate

Tests your grasp of FastAPI's dependency override mechanism. A strong answer mentions app.dependency_overrides, notes that sub-dependencies are bypassed, and stresses clearing overrides after each test.

WHAT THIS TESTS: This question probes whether you understand FastAPI's dependency injection system beyond basic endpoint decoration. The interviewer wants to know if you can isolate tests from external services like databases or auth providers without resorting to brittle global mocks. It also checks if you know the mechanics of app.dependency_overrides and the lifecycle implications of swapping dependencies.

A GOOD ANSWER COVERS: First, state that FastAPI exposes an app.dependency_overrides dictionary specifically for this purpose. Second, explain the syntax: app.dependency_overrides[original_dependency] = mock_dependency. Third, emphasize that when an override is active, FastAPI uses the replacement function exclusively and never executes the original dependency or any of its sub-dependencies. Fourth, note that the mock should return a value with the same type or interface the endpoint expects, because the path operation function still receives it as a parameter. Fifth, describe cleanup: clear the overrides dict after each test or use a pytest fixture to set and tear down the override so tests do not leak state.

COMMON WRONG ANSWERS: Monkeypatching the module where the dependency is defined instead of using FastAPI's override mechanism. This misses the point because the interviewer is explicitly testing framework knowledge. Another red flag is forgetting that sub-dependencies are bypassed; some candidates think they must mock the entire chain manually. A third mistake is applying overrides globally without resetting them, which causes confusing failures in later tests when the mock is still active.

LIKELY FOLLOW-UPS: How would you handle an async dependency override? What if the original dependency uses yield and has teardown logic; does the override need yield too? How do you structure this with a database dependency that returns a session object? Can you override dependencies per test rather than per module, and how do you ensure thread safety or async safety when doing so?

ONE CONCRETE EXAMPLE: Suppose you have a get_db dependency that yields a real SQLAlchemy session connected to PostgreSQL. In your test, define a mock_get_db function that yields an in-memory SQLite session or a mocked session object. In your test setup, write app.dependency_overrides[get_db] = mock_get_db. When the TestClient calls an endpoint that depends on get_db, FastAPI injects mock_get_db instead, skipping the real database connection entirely. After the test, call app.dependency_overrides.clear() so the next test starts fresh.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.