Challenge - 5 Problems
FastAPI TestClient Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI TestClient code?
Consider this FastAPI app and test code using TestClient. What will be the printed output?
FastAPI
from fastapi import FastAPI from fastapi.testclient import TestClient app = FastAPI() @app.get('/hello') def read_hello(): return {'message': 'Hello World'} client = TestClient(app) response = client.get('/hello') print(response.json())
Attempts:
2 left
💡 Hint
Think about what the endpoint returns and how TestClient calls it.
✗ Incorrect
The endpoint '/hello' returns a JSON with key 'message' and value 'Hello World'. TestClient calls this endpoint and response.json() returns that dictionary.
📝 Syntax
intermediate2:00remaining
Which option will cause a syntax error in this TestClient usage?
Identify which code snippet will cause a syntax error when using FastAPI's TestClient.
FastAPI
from fastapi import FastAPI from fastapi.testclient import TestClient app = FastAPI() @app.get('/items') def get_items(): return {'items': [1, 2, 3]} client = TestClient(app) # Which of these calls is invalid?
Attempts:
2 left
💡 Hint
Look carefully at the parentheses in each call.
✗ Incorrect
Option C is missing a closing parenthesis, causing a syntax error.
❓ state_output
advanced2:00remaining
What is the status code after this TestClient POST request?
Given this FastAPI app and test code, what will be the status code of the response?
FastAPI
from fastapi import FastAPI from fastapi.testclient import TestClient app = FastAPI() @app.post('/submit') def submit_data(): return {'result': 'success'} client = TestClient(app) response = client.post('/submit') status = response.status_code print(status)
Attempts:
2 left
💡 Hint
Check the HTTP method and the route defined.
✗ Incorrect
The POST request to '/submit' matches the route and returns a 200 OK status by default.
🔧 Debug
advanced2:00remaining
Why does this TestClient call raise a KeyError?
This FastAPI app returns a JSON. The test code tries to access a key from the response JSON but raises a KeyError. Why?
FastAPI
from fastapi import FastAPI from fastapi.testclient import TestClient app = FastAPI() @app.get('/data') def get_data(): return {'name': 'Alice', 'age': 30} client = TestClient(app) response = client.get('/data') print(response.json()['email'])
Attempts:
2 left
💡 Hint
Check the keys in the returned dictionary.
✗ Incorrect
The JSON returned has keys 'name' and 'age' but no 'email', so accessing 'email' causes KeyError.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI TestClient is true?
Select the correct statement about FastAPI's TestClient behavior.
Attempts:
2 left
💡 Hint
Think about how TestClient interacts with the FastAPI app internally.
✗ Incorrect
TestClient runs the app in the same process and calls endpoints directly without needing a server.