0
0
FastAPIframework~10 mins

Why testing ensures reliability in FastAPI - Test Your Understanding

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

Complete the code to import the FastAPI class.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AFastAPI
BRequest
CResponse
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
2fill in blank
medium

Complete the code to define a GET route that returns 'Hello World'.

FastAPI
@app.[1]("/")
async def read_root():
    return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Apost
Bput
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put instead of get for a simple fetch route.
3fill in blank
hard

Fix the error in the test function to check the response status code.

FastAPI
def test_read_root(client):
    response = client.get("/")
    assert response.[1] == 200
Drag options to blanks, or click blank then click option'
Astatus
Bstatus_code
Ccode
Dstatuscode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'status' or 'code' which do not exist on response.
4fill in blank
hard

Fill both blanks to create a test client and make a GET request.

FastAPI
from fastapi.testclient import [1]
client = [1](app)
response = client.[2]("/")
Drag options to blanks, or click blank then click option'
ATestClient
Bpost
Cget
DClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET request.
5fill in blank
hard

Fill all three blanks to write a test that asserts the JSON response message.

FastAPI
def test_read_root_message(client):
    response = client.get("/")
    assert response.json()[[1]] == [2]
    assert response.status_code == [3]
Drag options to blanks, or click blank then click option'
A"message"
B"Hello World"
C200
D"status"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON key or wrong status code.