Generate and review pytest unit tests for FastAPI endpoints. Use when writing, improving, or auditing API endpoint tests.
Generate well-structured pytest tests for FastAPI endpoints using TestClient and best practices.
Depends()). Determine which deps need mocking (DB sessions, auth, external clients).422401 / 403404 / 400pytest.fixture for reusable setup (test client, mock DB, auth override).test_<verb>_<resource>_<scenario>.from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_create_item_success():
response = client.post("/items/", json={"name": "widget", "price": 9.99})
assert response.status_code == 201
assert response.json()["name"] == "widget"
def test_create_item_missing_field():
response = client.post("/items/", json={"price": 9.99})
assert response.status_code == 422
app.dependency_overrides to swap FastAPI dependencies in tests.POST /users/ endpoint."