How would you use pytest fixtures to manage TestClient and mock dependencies?
Tests FastAPI test isolation and dependency overrides. Strong answer: function-scoped fixture yielding TestClient, mock injection via app.dependency_overrides, and teardown cleanup to prevent state leaks.
WHAT THIS TESTS: This question probes whether you understand test isolation in FastAPI and pytest fixture mechanics. The interviewer wants to see that you know TestClient wraps an ASGI app for synchronous HTTP calls, that FastAPI exposes app.dependency_overrides for swapping real dependencies with fakes, and that function-scoped fixtures with proper teardown prevent state leakage between tests. It also checks if you recognize that setup and teardown should live in one place rather than being copy-pasted across test functions.
A GOOD ANSWER COVERS: Four things in order. First, define a function-scoped pytest fixture, because the default scope ensures every test gets a fresh state. Second, inside the fixture instantiate TestClient by passing your FastAPI application directly, as the client holds an httpx session backed by the ASGI app. Third, inject mock dependencies by assigning fakes to app.dependency_overrides before yielding the client, so tests automatically receive the configured client. Fourth, perform teardown after the yield by clearing app.dependency_overrides in a finally block or after the yield statement, guaranteeing no mock state bleeds into the next test.
COMMON WRONG ANSWERS: Several anti-patterns signal weak experience. Using a module-scoped fixture for TestClient without cleanup means one test's mock overrides survive into another, causing flaky failures. Forgetting to reset dependency_overrides after yield is another frequent leak. Some candidates suggest monkeypatching globals or creating the client inside every test function, which defeats the purpose of fixtures and creates maintenance debt. Others confuse TestClient with an actual HTTP call and try to start a server, which is unnecessary.
LIKELY FOLLOW-UPS: Expect the interviewer to ask how you would handle database transactions or rollback in the same fixture pattern. They may also ask about async tests with AsyncClient, testing lifespan events, or how fixture composition works when you need separate mocks for authentication versus data access. Another common pivot is scope selection: when would you use session scope versus function scope for an expensive resource.
ONE CONCRETE EXAMPLE: Imagine a fixture named client. It imports app from main, creates a fake database session class, sets app.dependency_overrides to map get_db to that fake, then instantiates TestClient with the app. It yields the client so tests can call client.get or client.post. After yield, it runs app.dependency_overrides.clear in a finally block. This pattern gives every test a clean TestClient and guaranteed mock isolation.
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.