FastAPI is highly testable thanks to its TestClient (built on Starlette/httpx) and its dependency injection system, which makes mocking dependencies trivial. Tests typically use pytest and the TestClient to send requests to the app without a running server.
Basic endpoint testing with TestClient
fastapi.testclient TestClient
main app
client = TestClient(app)
():
response = client.get()
response.status_code ==
response.json() == {: }
():
response = client.post(, json={: , : })
response.status_code ==
response.json()[] ==
():
response = client.post(, json={: })
response.status_code ==
