FastAPI är mycket testbar tack vare sin TestClient (byggd på Starlette/httpx) och sitt dependency injection-system, som gör att mocka beroenden är trivialt. Tester använder vanligtvis pytest och TestClient för att skicka förfrågningar till appen utan en köra server.
Grundläggande endpoint-testning 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 ==
