FastAPI - Test Hello World


Writing the web application is nice, but we better also write tests that verify the application works properly. This will make it easier to verify that none of the changes we introduce later on will break parts that have been working and tested already.

The key here is to have a file that starts with test_ that has a function name that starts with test_ that uses assert to check values.


examples/fastapi/hello-world/test_main.py
from fastapi.testclient import TestClient

from main import app

client = TestClient(app)


def test_read_main():
    response = client.get("/")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    assert response.json() == {"message": "Hello World"}

Just run pytest to execute the tests.


pytest