Как тестировать API в Python?

Middle
257 просмотров
AFK Offer AI

FastAPI: TestClient (sync) или AsyncClient (async). from fastapi.testclient import TestClient; client = TestClient(app); r = client.get("/users"); assert r.status_code == 200. pytest fixtures: @pytest.fixture def client(): return TestClient(app). httpx.AsyncClient(app=app) — для async тестов. Моки: respx для httpx, responses для requests — мокируют внешние API. pytest-httpx — автоматические моки. Стратегия: unit (mock repos) → integration (test DB) → e2e (real server). Contract testing: schemathesis — автоматические тесты из OpenAPI schema. Postman/Newman — для ручного + CI тестирования.

Следующий вопрос

Что такое Clean Architecture в контексте Python?