FastAPI ทดสอบได้สูงเพราะ TestClient (สร้างบน Starlette/httpx) และระบบ dependency injection ของมัน ซึ่งทำให้การ mock dependencies ง่ายมาก การทดสอบมักใช้ pytest และ TestClient เพื่อส่ง request ไปยังแอปโดยไม่ต้องมี server ที่รันอยู่
การทดสอบ endpoint พื้นฐานด้วย 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 ==
