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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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.
A 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.
Interview question
Which pytest fixture pattern correctly provides each FastAPI test with a fresh TestClient and isolated mock dependencies?
- a.Use a module-scoped fixture that instantiates TestClient(app) once and leaves dependency_overrides in place
- b.Instantiate TestClient(app) inside each test function and manually inject mocks before every request
- c.Use a function-scoped fixture that monkeypatches global dependency functions and returns TestClient(app)
- d.Use a function-scoped fixture that assigns fakes to app.dependency_overrides, yields TestClient(app), and clears overrides after yieldCorrect
Why? this is the answer
The correct pattern combines function scope (fresh state per test), FastAPI's dependency_overrides, and post-yield cleanup to prevent leaks. A module-scoped fixture is tempting for performance but causes one test's mock overrides to survive into another, creating flaky failures.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles