0
0
FastAPIframework~20 mins

Testing path operations in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Path Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response status code when a GET request is made to this FastAPI path operation?

Consider this FastAPI path operation:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

What status code will the response have when requesting /items/5?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
A200
B404
C500
D302
Attempts:
2 left
💡 Hint

By default, successful GET requests return a standard success status code.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a POST path operation with a JSON body in FastAPI?

Which of the following code snippets correctly defines a POST path operation that accepts a JSON body with a name field?

A
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.post("/items")
async def create_item(item: Item):
    return item
B
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
async def create_item(name: str):
    return {"name": name}
C
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
async def create_item():
    name = request.json()["name"]
    return {"name": name}
D
from fastapi import FastAPI
app = FastAPI()

@app.post("/items")
def create_item(item: dict):
    return item
Attempts:
2 left
💡 Hint

FastAPI uses Pydantic models to parse and validate JSON bodies.

state_output
advanced
2:00remaining
What is the output of this test client code for a FastAPI GET path operation?

Given this FastAPI app and test code, what will print(response.json()) output?

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    return {"user_id": user_id, "name": f"User{user_id}"}

client = TestClient(app)
response = client.get("/users/10")
print(response.json())
FastAPI
from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    return {"user_id": user_id, "name": f"User{user_id}"}

client = TestClient(app)
response = client.get("/users/10")
print(response.json())
A{"user_id": 10, "name": "user10"}
B{"user_id": "10", "name": "User10"}
C{"user_id": 10, "name": "User10"}
DTypeError: Object of type int is not JSON serializable
Attempts:
2 left
💡 Hint

FastAPI automatically converts path parameters to the declared type and serializes the response to JSON.

🔧 Debug
advanced
2:00remaining
Which option causes a 422 Unprocessable Entity error when testing this FastAPI POST path operation?

Given this FastAPI POST path operation:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    age: int

@app.post("/users")
async def create_user(user: User):
    return user

Which test client request will cause a 422 error?

Aclient.post("/users", json={"username": "alice", "age": 30})
Bclient.post("/users", json={"username": "bob", "age": "thirty"})
Cclient.post("/users", json={"username": "carol", "age": 25})
Dclient.post("/users", json={"username": "dave", "age": 40})
Attempts:
2 left
💡 Hint

Check the data types expected by the Pydantic model.

🧠 Conceptual
expert
3:00remaining
What happens if you call a FastAPI path operation function directly instead of using TestClient?

Consider this FastAPI path operation:

from fastapi import FastAPI
app = FastAPI()

@app.get("/hello")
async def say_hello():
    return {"message": "Hello"}

What is the difference between calling await say_hello() directly in Python and calling client.get('/hello') using TestClient?

ATestClient returns the Python dict directly; calling the function returns a Response object.
BBoth return the exact same Response object with status code and headers.
CCalling the function directly raises an error because it requires a request object; TestClient works fine.
DCalling the function directly returns the Python dict; TestClient returns a Response object with JSON content and status code.
Attempts:
2 left
💡 Hint

Think about what FastAPI does to convert function return values into HTTP responses.