0
0
FastAPIframework~20 mins

Overriding dependencies in tests in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Dependency Override Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when overriding a dependency in FastAPI test client?

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?

FastAPI
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())
A{"user_id": 42}
B{"user_id": 99}
C{"user_id": null}
DRaises a RuntimeError
Attempts:
2 left
💡 Hint

Think about what the override function returns and how FastAPI uses dependency overrides in tests.

📝 Syntax
intermediate
2:00remaining
Which option correctly overrides a FastAPI dependency in a test?

Choose the correct way to override the get_db dependency in a FastAPI test.

FastAPI
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)
Aapp.dependency_overrides[get_db] = lambda: "test_db"
Bapp.override_dependency(get_db, lambda: "test_db")
Capp.dependency_overrides[get_db] = "test_db"
Dapp.dependency_overrides[get_db()] = lambda: "test_db"
Attempts:
2 left
💡 Hint

Remember the syntax for dependency_overrides dictionary keys and values.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI test fail with a 422 error despite overriding the dependency?

Consider this FastAPI endpoint and test. The test overrides the dependency but the response status code is 422. Why?

FastAPI
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)
ABecause the endpoint requires authentication headers missing in the test
BBecause the dependency override was not applied correctly
CBecause the overridden dependency returns None, which FastAPI rejects for a required str parameter
DBecause the test client is not instantiated properly
Attempts:
2 left
💡 Hint

Think about how FastAPI validates dependency return values for required parameters.

state_output
advanced
2:00remaining
What is the value of the global variable after running this FastAPI test with dependency override?

Given this code, what is the value of state['count'] after the test client calls the endpoint?

FastAPI
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")
A10
B0
C1
D11
Attempts:
2 left
💡 Hint

Consider how the override changes the state and what the original function did before.

🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI dependency overrides in tests is TRUE?

Choose the correct statement about how FastAPI handles dependency overrides during testing.

ADependency overrides can only override async dependencies, not sync ones
BDependency overrides only apply to the first request made by the test client
CDependency overrides automatically reset after each test function when using TestClient
DDependency overrides affect all requests globally until manually cleared or the app is restarted
Attempts:
2 left
💡 Hint

Think about the scope and lifetime of app.dependency_overrides.