FastAPI由于其**TestClient(基于Starlette/httpx构建)和其依赖注入**系统而具有高度的可测试性,这使模拟依赖变得轻而易举。测试通常使用pytest和TestClient向应用程序发送请求,无需运行服务器。
使用TestClient的基本端点测试
python
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 ==
