Consider a FastAPI endpoint that should return status code 200 but mistakenly returns 404. What will the client receive?
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): return {"item_id": item_id, "status": "not found"}, 404
Check how FastAPI handles return values with status codes.
FastAPI allows returning a tuple (response, status_code). Here, the status code 404 is sent with the JSON body, so the client gets both the data and the 404 status.
Given a FastAPI endpoint that raises a ValueError, what will a test client receive?
from fastapi import FastAPI app = FastAPI() @app.get("/error") async def error_endpoint(): raise ValueError("Invalid value")
Consider what happens when an unhandled exception occurs in FastAPI.
When an unhandled exception like ValueError is raised, FastAPI returns a 500 Internal Server Error by default.
Choose the correct test code snippet that sends JSON data to a FastAPI POST endpoint and checks the response status.
from fastapi.testclient import TestClient from main import app client = TestClient(app) def test_create_item(): response = client.post("/items/", json={"name": "Book", "price": 10.5}) assert response.status_code == 201
Check how to send JSON data in FastAPI test client.
FastAPI test client requires the json parameter to send JSON data. Using data sends form data, which is incorrect here.
Given this test code, why does the response status code return 422 Unprocessable Entity?
response = client.post("/users/", json={"username": "", "age": 25}) assert response.status_code == 201
Check the data validation constraints on the username field.
FastAPI uses Pydantic models that validate input. An empty username likely violates a non-empty string constraint, causing 422.
Which statement best explains why automated tests help keep a FastAPI app reliable?
Think about the role of automated tests in software development.
Automated tests run code checks early and often, catching bugs before deployment, which improves reliability. They do not replace manual testing or code reviews.