Given this FastAPI app with a dependency that returns a user ID, and a test that overrides this dependency, what will the test client receive as the response?
from fastapi import FastAPI, Depends from fastapi.testclient import TestClient app = FastAPI() def get_user_id(): return 42 @app.get("/user") async def read_user(user_id: int = Depends(get_user_id)): return {"user_id": user_id} client = TestClient(app) def override_get_user_id(): return 99 app.dependency_overrides[get_user_id] = override_get_user_id response = client.get("/user") print(response.json())
Think about what the override function returns and how FastAPI uses dependency overrides in tests.
The test overrides the get_user_id dependency with override_get_user_id which returns 99. So the endpoint returns {"user_id": 99}.
Choose the correct way to override the get_db dependency in a FastAPI test.
def get_db(): return "real_db" app = FastAPI() @app.get("/items") async def read_items(db=Depends(get_db)): return {"db": db} client = TestClient(app)
Remember the syntax for dependency_overrides dictionary keys and values.
Option A correctly assigns a lambda function to override the get_db dependency. Option A uses a non-existent method. Option A uses a call instead of the function as key. Option A assigns a string instead of a callable.
Consider this FastAPI endpoint and test. The test overrides the dependency but the response status code is 422. Why?
from fastapi import FastAPI, Depends from fastapi.testclient import TestClient app = FastAPI() def get_token(): return "real_token" @app.get("/secure") async def secure_route(token: str = Depends(get_token)): return {"token": token} client = TestClient(app) def override_token(): return None app.dependency_overrides[get_token] = override_token response = client.get("/secure") print(response.status_code)
Think about how FastAPI validates dependency return values for required parameters.
The overridden dependency returns None, but the endpoint expects a non-optional string. FastAPI raises a 422 Unprocessable Entity error because None is invalid for a required str parameter.
Given this code, what is the value of state['count'] after the test client calls the endpoint?
from fastapi import FastAPI, Depends from fastapi.testclient import TestClient app = FastAPI() state = {"count": 0} def increment(): state["count"] += 1 return state["count"] @app.get("/count") async def get_count(count=Depends(increment)): return {"count": count} client = TestClient(app) def override_increment(): state["count"] += 10 return state["count"] app.dependency_overrides[increment] = override_increment client.get("/count")
Consider how the override changes the state and what the original function did before.
The original increment adds 1 to count. The override adds 10 instead. Since state['count'] starts at 0, after the override runs once, it becomes 10. The original increment was never called, so count is 10 after the override. The endpoint returns 10, so state['count'] is 10 after the call.
Choose the correct statement about how FastAPI handles dependency overrides during testing.
Think about the scope and lifetime of app.dependency_overrides.
Dependency overrides are stored in a dictionary on the app instance and affect all requests globally until cleared. They do not reset automatically after each test or only apply once. Both sync and async dependencies can be overridden.