0
0
FastAPIframework~10 mins

TestClient basics 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 import the TestClient from FastAPI's testing module.

FastAPI
from fastapi.testclient import [1]
Drag options to blanks, or click blank then click option'
ATestClient
BTestAPIClient
CFastClient
DClientTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'ClientTest' or 'FastClient'.
Trying to import from 'fastapi.client' instead of 'fastapi.testclient'.
2fill in blank
medium

Complete the code to create a TestClient instance for the FastAPI app named 'app'.

FastAPI
client = [1](app)
Drag options to blanks, or click blank then click option'
ATestClient
BClient
CFastClient
DAppClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Client(app)' which is not defined.
Trying to instantiate 'FastClient' or 'AppClient' which do not exist.
3fill in blank
hard

Fix the error in the code to send a GET request to '/' using the TestClient instance named 'client'.

FastAPI
response = client.[1]("/")
Drag options to blanks, or click blank then click option'
Apost
Bsend
Cget
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET request.
Using 'send' which is not a method of TestClient.
Using 'request' without specifying the HTTP method.
4fill in blank
hard

Fill both blanks to check if the response status code is 200 and the response JSON has a key 'message'.

FastAPI
assert response.status_code == [1]
assert 'message' [2] response.json()
Drag options to blanks, or click blank then click option'
A200
Bin
Cnot in
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for status code 404 which means not found.
Using 'not in' which would fail the assertion if 'message' is present.
5fill in blank
hard

Fill all three blanks to send a POST request with JSON data {'name': 'Alice'} and check if the response JSON contains the same name.

FastAPI
response = client.[1]("/users", json=[2])
assert response.json().get('name') == [3]
Drag options to blanks, or click blank then click option'
Apost
B{'name': 'Alice'}
C'Alice'
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post' to send data.
Passing JSON data as a string instead of a dictionary.
Comparing the response name to a wrong value.