Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the FastAPI class.
FastAPI
from fastapi import [1] app = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
✗ Incorrect
We import FastAPI to create the app instance.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put instead of get for a simple fetch route.
✗ Incorrect
The get decorator defines a GET HTTP method route.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'status' or 'code' which do not exist on response.
✗ Incorrect
The correct attribute to check HTTP status is status_code.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET request.
✗ Incorrect
TestClient creates the test client, and get makes the GET request.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON key or wrong status code.
✗ Incorrect
The JSON key is 'message', the expected value is 'Hello World', and the status code is 200.