How do router-level and app-level dependencies affect dependency override testing?
This tests FastAPI dependency hierarchy and test isolation. A strong answer states overrides are global to the app, so router-specific deps need scoped fixtures to prevent cross-test leaks. A red flag is claiming APIRouter has its own override registry.
WHAT THIS TESTS: FastAPI dependency injection resolution order and the global nature of app.dependency_overrides. Interviewers want to know if you understand that APIRouter does not maintain a separate container; dependencies declared on a router are resolved through the main app. This matters because in a large codebase with many routers, naive override strategies can leak state between tests or accidentally affect unrelated routes.
A GOOD ANSWER COVERS: First, state that dependency overrides in FastAPI are always applied to the app instance, never to an APIRouter directly. Second, explain that router-level dependencies are overridden by assigning to app.dependency_overrides[original_dep] = mock_dep before the TestClient is initialized or during a fixture setup. Third, describe the importance of cleanup: overrides persist on the app object, so you must reset app.dependency_overrides.clear() or restore the original mapping after each test to prevent cross-test contamination. Fourth, mention that if a dependency is used in only one router, you can still override it globally on the app, but you should isolate the override using a pytest fixture with setup and teardown or a context manager wrapper. Fifth, note that creating separate app instances per router for testing is an anti-pattern because it diverges from production wiring; instead, use the real app but manage override state carefully. Sixth, add that you can verify the override is active by inspecting app.dependency_overrides inside the fixture or by asserting on the mock call count in the test.
COMMON WRONG ANSWERS: Claiming that APIRouter has its own dependency_overrides dictionary. Suggesting that you must override the dependency on every individual path operation instead of once at the app level. Proposing to mutate the router object directly rather than using the app's override registry. Forgetting to clean up overrides, which causes flaky tests when the test runner reuses the app instance. Asserting that router-level dependencies are invisible to the main app and therefore cannot be overridden. Recommending monkeypatching the dependency function in its module instead of using the supported override mechanism, which breaks when FastAPI caches the dependency callable.
LIKELY FOLLOW-UPS: How would you override a dependency that uses yield and has cleanup logic? What happens if two routers use the same dependency name but different functions? How do you handle overrides when running tests in parallel with pytest-xdist? Would you structure your app differently to make testing easier, such as using a factory pattern?
ONE CONCRETE EXAMPLE: Suppose you have an APIRouter for billing with a router-level dependency get_payment_gateway. In your main app, you include the router and its dependency. During testing, you write a fixture that sets app.dependency_overrides[get_payment_gateway] = lambda: MockGateway(), yields the TestClient, and then calls app.dependency_overrides.clear() in the teardown. This ensures that only tests using that fixture see the override, and subsequent tests receive the real dependency. If you skipped the cleanup, a test in the user router might accidentally inherit the mock gateway, causing a false pass.
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.