FastAPI er meget testbar takket være dets TestClient (bygget på Starlette/httpx) og dets dependency injection-system, som gør mock af dependencies trivielt. Tests bruger typisk pytest og TestClient til at sende requests til appen uden en kørende server.
Grundlæggende endpoint-test med 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 ==
