A. The path parameter expects an int, but 'abc' is a string causing a validation error.
B. The endpoint URL is missing a slash.
C. TestClient is not imported correctly.
D. The function read_item does not return a dictionary.
Solution
Step 1: Check the path parameter type and request URL
The endpoint expects an integer for item_id, but the test sends 'abc' which is a string.
Step 2: Understand FastAPI validation behavior
FastAPI will reject the request with a 422 Unprocessable Entity error due to type mismatch.
Final Answer:
The path parameter expects an int, but 'abc' is a string causing a validation error. -> Option A
Quick Check:
Type mismatch in path parameter causes validation error [OK]
Hint: Check parameter types match URL values [OK]
Common Mistakes:
Ignoring type mismatch in path parameters
Assuming missing slash causes error
Thinking TestClient import is wrong
5. You want to ensure your FastAPI app reliably returns a JSON with a 'status' key set to 'ok' for the root endpoint. Which test code snippet correctly verifies this behavior?
hard
A. response = client.get("/status")
assert response.json() == {"status": "ok"}
B. response = client.post("/")
assert response.status_code == 404
C. response = client.get("/")
assert response.json() == {"status": "ok"}
D. response = client.get("/")
assert response.text == "status: ok"
Solution
Step 1: Identify the correct HTTP method and endpoint
The root endpoint "/" is usually accessed with GET to check status.
Step 2: Verify the response JSON content
response = client.get("/")
assert response.json() == {"status": "ok"} correctly sends GET to "/" and asserts the JSON response matches {"status": "ok"}.