0
0
FastAPIframework~20 mins

TestClient basics in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI TestClient Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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())
A{'message': 'Hello World'}
B{"error": "Not Found"}
CNone
DSyntaxError
Attempts:
2 left
💡 Hint
Think about what the endpoint returns and how TestClient calls it.
📝 Syntax
intermediate
2: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?
Aresponse = client.get('/items')
Bresponse = client.post('/items')
Cresponse = client.get('/items' # missing closing parenthesis
Dresponse = client.get('/items', params={'q': 'test'})
Attempts:
2 left
💡 Hint
Look carefully at the parentheses in each call.
state_output
advanced
2: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)
A405
B200
C500
D404
Attempts:
2 left
💡 Hint
Check the HTTP method and the route defined.
🔧 Debug
advanced
2: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'])
AThe TestClient was not initialized properly.
BThe response is not JSON, so .json() fails.
CThe endpoint '/data' does not exist.
DThe key 'email' does not exist in the returned JSON.
Attempts:
2 left
💡 Hint
Check the keys in the returned dictionary.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI TestClient is true?
Select the correct statement about FastAPI's TestClient behavior.
ATestClient allows calling FastAPI endpoints without starting an actual server.
BTestClient runs the FastAPI app in a separate server process during tests.
CTestClient requires the app to be running on localhost before tests.
DTestClient can only test GET requests, not POST or others.
Attempts:
2 left
💡 Hint
Think about how TestClient interacts with the FastAPI app internally.