0
0
FastAPIframework~10 mins

Testing path operations in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a test client for the FastAPI app.

FastAPI
from fastapi.testclient import TestClient
from main import app

client = [1](app)
Drag options to blanks, or click blank then click option'
AAppClient
BClient
CFastClient
DTestClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name like Client or FastClient.
Not importing TestClient before using it.
2fill in blank
medium

Complete the code to send a GET request to the '/items' path.

FastAPI
response = client.[1]('/items')
Drag options to blanks, or click blank then click option'
Aget
Bput
Cpost
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put instead of get for a GET request.
Forgetting to include the path string.
3fill in blank
hard

Fix the error in the assertion to check the response status code is 200.

FastAPI
assert response.status_code == [1]
Drag options to blanks, or click blank then click option'
A200
B404
C201
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 201 which means created, not for GET.
Using 404 which means not found.
4fill in blank
hard

Fill both blanks to test a POST request with JSON data and check the response status code.

FastAPI
response = client.[1]('/items', json=[2])
assert response.status_code == 201
Drag options to blanks, or click blank then click option'
Apost
Bget
C{'name': 'test item', 'price': 10.5}
D['name', 'test item']
Attempts:
3 left
💡 Hint
Common Mistakes
Using get instead of post for sending data.
Passing JSON as a list instead of a dictionary.
5fill in blank
hard

Fill all three blanks to test a PUT request updating an item and verify the response JSON content.

FastAPI
response = client.[1]('/items/1', json=[2])
assert response.status_code == 200
assert response.json() == [3]
Drag options to blanks, or click blank then click option'
Aput
B{'name': 'updated item', 'price': 15.0}
C{'id': 1, 'name': 'updated item', 'price': 15.0}
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of put for updating.
Not matching the response JSON with expected updated data.