FastAPI قابل للاختبار بدرجة عالية بفضل TestClient (المبني على Starlette/httpx) ونظام حقن التبعيات الخاص به، مما يجعل محاكاة التبعيات سهلة جداً. عادةً ما تستخدم الاختبارات pytest وTestClient لإرسال طلبات إلى التطبيق دون الحاجة لخادم قيد التشغيل.
اختبار نقاط النهاية الأساسية باستخدام 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 ==
