
Pytest Patterns
- 91 installs
- 8 repo stars
- Updated February 6, 2026
- hieutrtr/ai1-skills
Python backend testing patterns with pytest for FastAPI: unit tests, async integration tests with httpx, fixtures, factory_boy, and mocking.
About
Covers pytest patterns for FastAPI including unit tests for services and repositories, async integration tests with httpx.AsyncClient, fixtures, factory_boy, and parametrized tests. A developer uses it when writing Python backend tests.
- Async testing with pytest-asyncio and httpx.AsyncClient
- Test organization (unit/integration), conftest hierarchy, and factory_boy
Pytest Patterns by the numbers
- 91 all-time installs (skills.sh)
- +4 installs in the week ending Aug 2, 2026 (Skillselion tracking)
- Ranked #1,036 of 2,153 Testing & QA skills by installs in the Skillselion catalog
- Data as of Aug 2, 2026 (Skillselion catalog sync)
npx skills add https://github.com/hieutrtr/ai1-skills --skill pytest-patternsAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 91 |
|---|---|
| repo stars | ★ 8 |
| Last updated | February 6, 2026 |
| Repository | hieutrtr/ai1-skills ↗ |
What it does
Python backend testing patterns with pytest for FastAPI: unit tests, async integration tests with httpx, fixtures, factory_boy, and mocking.
Files
Pytest Patterns
When to Use
Activate this skill when:
- Writing unit tests for service or repository classes
- Writing integration tests for FastAPI endpoints with httpx.AsyncClient
- Creating or refactoring pytest fixtures and conftest files
- Setting up factory_boy factories for test data
- Testing async code with pytest-asyncio
- Mocking external services (HTTP APIs, email, queues)
- Adding parametrized tests for input variations
- Auditing or improving test coverage
Do NOT use this skill for:
- Frontend React component or hook tests (use
react-testing-patterns) - E2E browser tests with Playwright (use
e2e-testing) - TDD red-green-refactor workflow enforcement (use
tdd-workflow) - Writing application code (use
python-backend-expert)
Instructions
Test Organization
tests/
├── conftest.py # Root conftest: DB session, async client, auth helpers
├── unit/
│ ├── conftest.py # Unit-specific fixtures (mocked repos, services)
│ ├── services/
│ │ ├── test_user_service.py
│ │ └── test_order_service.py
│ └── repositories/
│ └── test_user_repository.py
├── integration/
│ ├── conftest.py # Integration-specific fixtures (test DB, seeding)
│ ├── test_users_api.py
│ └── test_orders_api.py
└── factories/
├── __init__.py
├── user_factory.py
└── order_factory.pyNaming conventions:
- Test files:
test_<module>.py - Test classes:
Test<Feature>(group related tests, no__init__) - Test functions:
test_<action>_<expected_outcome>ortest_<scenario> - Fixtures: descriptive noun (
db_session,authenticated_client,sample_user)
Marker conventions:
# pyproject.toml
[tool.pytest.ini_options]
markers = [
"unit: Unit tests (no DB, no network)",
"integration: Integration tests (real DB, real HTTP)",
"slow: Tests that take > 1 second",
]
asyncio_mode = "auto"Run subsets: pytest -m unit, pytest -m integration, pytest -m "not slow".
Fixture Architecture
Conftest Hierarchy
Fixtures cascade: root conftest.py provides shared fixtures; subdirectory conftest files add layer-specific fixtures.
Root conftest (tests/conftest.py):
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from app.main import app
from app.database import get_db
@pytest.fixture(scope="session")
def anyio_backend():
return "asyncio"
@pytest.fixture(scope="session")
async def engine():
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
await engine.dispose()
@pytest.fixture
async def db_session(engine):
async with async_sessionmaker(engine, class_=AsyncSession)() as session:
yield session
await session.rollback()
@pytest.fixture
async def client(db_session):
async def override_get_db():
yield db_session
app.dependency_overrides[get_db] = override_get_db
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()Fixture Scopes
| Scope | Use For | Example |
|---|---|---|
function (default) | Isolated per-test data | db_session, sample_user |
class | Shared across test class | service_instance |
module | Shared across test file | seeded_database |
session | Shared across entire run | engine, anyio_backend |
Rules:
- Default to
functionscope for data isolation - Use
sessionscope only for expensive, stateless resources (engine, event loop) - Never use
sessionscope for mutable data -- tests will interfere with each other - Fixtures that yield must clean up (rollback, delete, close)
Auth Fixtures
@pytest.fixture
def auth_headers():
"""Return authorization headers for a standard test user."""
token = create_test_token(user_id=1, role="member")
return {"Authorization": f"Bearer {token}"}
@pytest.fixture
async def authenticated_client(client, auth_headers):
"""AsyncClient pre-configured with auth headers."""
client.headers.update(auth_headers)
return client
@pytest.fixture
def admin_headers():
"""Return authorization headers for an admin user."""
token = create_test_token(user_id=99, role="admin")
return {"Authorization": f"Bearer {token}"}Factory Pattern
Use factory_boy for consistent, overridable test data.
import factory
from app.models import User, Order
class UserFactory(factory.Factory):
class Meta:
model = User
id = factory.Sequence(lambda n: n + 1)
email = factory.LazyAttribute(lambda o: f"user{o.id}@example.com")
display_name = factory.Faker("name")
role = "member"
is_active = True
class OrderFactory(factory.Factory):
class Meta:
model = Order
id = factory.Sequence(lambda n: n + 1)
user_id = factory.LazyAttribute(lambda o: UserFactory().id)
total_cents = factory.Faker("random_int", min=100, max=100000)
status = "pending"Usage in tests:
def test_user_defaults():
user = UserFactory()
assert user.is_active is True
assert user.role == "member"
def test_user_override():
admin = UserFactory(role="admin", display_name="Admin User")
assert admin.role == "admin"
def test_user_batch():
users = UserFactory.build_batch(5)
assert len(users) == 5SQLAlchemy integration (for integration tests that persist to DB):
class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = User
sqlalchemy_session = None # Set per-test via conftest
# ... fields same as aboveSet session in conftest:
@pytest.fixture(autouse=True)
def set_factory_session(db_session):
UserFactory._meta.sqlalchemy_session = db_session
OrderFactory._meta.sqlalchemy_session = db_sessionAPI Integration Tests
Test FastAPI endpoints with httpx.AsyncClient against the real app, but with a test database.
import pytest
from httpx import AsyncClient
class TestUsersAPI:
"""Integration tests for /api/v1/users endpoints."""
async def test_create_user_success(self, authenticated_client: AsyncClient):
response = await authenticated_client.post("/api/v1/users", json={
"email": "new@example.com",
"display_name": "New User",
})
assert response.status_code == 201
data = response.json()
assert data["email"] == "new@example.com"
assert "id" in data
async def test_create_user_duplicate_email(self, authenticated_client, sample_user):
response = await authenticated_client.post("/api/v1/users", json={
"email": sample_user.email,
"display_name": "Duplicate",
})
assert response.status_code == 409
assert "already exists" in response.json()["detail"]
async def test_list_users_pagination(self, authenticated_client):
response = await authenticated_client.get("/api/v1/users?limit=10&cursor=0")
assert response.status_code == 200
data = response.json()
assert "items" in data
assert "next_cursor" in data
async def test_get_user_not_found(self, authenticated_client):
response = await authenticated_client.get("/api/v1/users/99999")
assert response.status_code == 404
async def test_unauthenticated_request(self, client):
response = await client.get("/api/v1/users")
assert response.status_code == 401Key patterns:
- Use
authenticated_clientfor protected endpoints, plainclientfor auth testing - Assert status code first, then response body
- Test error paths: 404, 409, 422, 401, 403
- Test pagination parameters
- Never assert on exact timestamps or auto-generated IDs (use
"id" in data)
Async Tests
With asyncio_mode = "auto" in pyproject.toml, all async def test_* functions run automatically.
async def test_async_service_call(db_session):
service = UserService(db_session)
user = await service.create_user(email="test@example.com", display_name="Test")
assert user.id is not None
async def test_concurrent_operations(db_session):
service = UserService(db_session)
import asyncio
results = await asyncio.gather(
service.get_user(1),
service.get_user(2),
service.get_user(3),
)
assert len(results) == 3Common pitfalls:
- Do NOT mix
syncandasyncfixtures carelessly -- an async fixture can only be used by async tests - Always use
pytest-asyncio(notanyiodirectly) for consistency - If a test hangs, check for un-awaited coroutines or missing
asynckeywords
Mocking Strategy
Mock external services -- YES:
from unittest.mock import AsyncMock, patch
async def test_send_notification(db_session):
with patch("app.services.notification.EmailClient") as mock_email:
mock_email.return_value.send = AsyncMock(return_value=True)
service = NotificationService(db_session)
result = await service.notify_user(user_id=1, message="Hello")
assert result is True
mock_email.return_value.send.assert_called_once()Mock the database -- NO:
# BAD: Mocking the database hides real query issues
async def test_user_service(mock_db):
mock_db.execute.return_value = MockResult([user_dict]) # Don't do this
# GOOD: Use a real test database (SQLite or PostgreSQL in Docker)
async def test_user_service(db_session):
service = UserService(db_session)
user = await service.create_user(email="test@example.com", display_name="Test")
fetched = await service.get_user(user.id)
assert fetched.email == "test@example.com"What to mock:
| Mock | Do Not Mock |
|---|---|
HTTP APIs (use respx or unittest.mock) | Database queries |
| Email/SMS services | SQLAlchemy sessions |
| File storage (S3, GCS) | Repository methods (in integration tests) |
| Message queues (Redis, RabbitMQ) | Pydantic validation |
Time/datetime (freezegun) | FastAPI dependency injection |
| Random/UUID generation | ORM relationships |
`respx` for HTTP mocking:
import respx
from httpx import Response
@respx.mock
async def test_external_api_call():
respx.get("https://api.example.com/data").mock(
return_value=Response(200, json={"key": "value"})
)
service = ExternalDataService()
result = await service.fetch_data()
assert result["key"] == "value"Parametrized Tests
Use @pytest.mark.parametrize for testing multiple input/output combinations:
@pytest.mark.parametrize("email,is_valid", [
("user@example.com", True),
("user@sub.domain.com", True),
("user+tag@example.com", True),
("", False),
("not-an-email", False),
("@missing-local.com", False),
("user@", False),
])
def test_email_validation(email, is_valid):
if is_valid:
assert validate_email(email) is True
else:
with pytest.raises(ValidationError):
validate_email(email)Parametrize with IDs for readable output:
@pytest.mark.parametrize("status,expected_code", [
pytest.param("active", 200, id="active-user-ok"),
pytest.param("suspended", 403, id="suspended-user-forbidden"),
pytest.param("deleted", 404, id="deleted-user-not-found"),
])
async def test_user_access_by_status(authenticated_client, status, expected_code):
...Parametrize multiple fixtures:
@pytest.mark.parametrize("role,can_delete", [
("admin", True),
("member", False),
("viewer", False),
])
async def test_delete_permission(client, role, can_delete):
headers = {"Authorization": f"Bearer {create_test_token(role=role)}"}
response = await client.delete("/api/v1/users/1", headers=headers)
if can_delete:
assert response.status_code == 204
else:
assert response.status_code == 403Coverage Requirements
Minimum thresholds:
- Overall: 80% line coverage
- Service layer: 90% (critical business logic)
- Repository layer: 70% (straightforward CRUD)
- Routes: 80% (all success + primary error paths)
pyproject.toml configuration:
[tool.coverage.run]
source = ["app"]
omit = ["app/migrations/*", "app/main.py", "app/__init__.py"]
[tool.coverage.report]
fail_under = 80
show_missing = true
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
"if __name__ ==",
"@overload",
]Running coverage:
pytest --cov=app --cov-report=term-missing --cov-report=html --cov-fail-under=80Use scripts/check-test-coverage.sh to automate coverage checks with report output.
Test Anti-Patterns
1. Testing implementation details:
# BAD: Tests internal method calls
def test_service_calls_repo(mock_repo):
service.create_user(data)
mock_repo.insert.assert_called_once_with(...)
# GOOD: Tests observable behavior
async def test_create_user(db_session):
service = UserService(db_session)
user = await service.create_user(email="a@b.com", display_name="A")
fetched = await service.get_user(user.id)
assert fetched is not None2. Shared mutable state between tests:
# BAD: Module-level mutable data
users = []
def test_add_user():
users.append(User(id=1)) # Leaks to other tests
# GOOD: Use fixtures with function scope
@pytest.fixture
def users():
return [UserFactory()]3. Overly broad assertions:
# BAD
assert response.status_code == 200 # Only checks status
# GOOD
assert response.status_code == 200
data = response.json()
assert data["email"] == "test@example.com"
assert data["role"] == "member"4. Missing error path tests: Every endpoint should have tests for at least: success, not found, validation error, and unauthorized.
Examples
See references/conftest-template.py for production conftest setup. See references/factory-template.py for factory_boy patterns. See references/api-test-template.py for API integration test patterns. See references/service-test-template.py for service unit test patterns. See references/integration-test-template.py for full integration test patterns.
"""
api-test-template.py — Annotated API integration test using httpx.AsyncClient.
Place at: tests/integration/test_users_api.py
This template demonstrates:
- Testing all CRUD endpoints for a resource
- Auth/unauth request paths
- Error response assertions (404, 409, 422)
- Pagination testing
- Using fixtures for test data
Prerequisites:
- Root conftest.py provides: client, authenticated_client, admin_client, db_session
- factories/ provides: UserFactory
"""
import pytest
from httpx import AsyncClient
# ─── Mark the entire module as integration tests ─────────────────────────────────
pytestmark = [pytest.mark.integration]
class TestCreateUser:
"""POST /api/v1/users"""
async def test_create_user_success(self, authenticated_client: AsyncClient):
"""Authenticated user can create a new user with valid data."""
payload = {
"email": "newuser@example.com",
"display_name": "New User",
"role": "member",
}
response = await authenticated_client.post("/api/v1/users", json=payload)
# Assert status first, then body
assert response.status_code == 201
data = response.json()
assert data["email"] == "newuser@example.com"
assert data["display_name"] == "New User"
assert data["role"] == "member"
assert "id" in data # Don't assert exact ID
assert "created_at" in data # Don't assert exact timestamp
async def test_create_user_duplicate_email(
self, authenticated_client: AsyncClient, sample_user
):
"""Creating a user with an existing email returns 409 Conflict."""
payload = {
"email": sample_user.email, # Already exists
"display_name": "Duplicate",
}
response = await authenticated_client.post("/api/v1/users", json=payload)
assert response.status_code == 409
assert "already exists" in response.json()["detail"].lower()
async def test_create_user_invalid_email(self, authenticated_client: AsyncClient):
"""Invalid email format returns 422 Unprocessable Entity."""
payload = {
"email": "not-an-email",
"display_name": "Bad Email",
}
response = await authenticated_client.post("/api/v1/users", json=payload)
assert response.status_code == 422
async def test_create_user_unauthenticated(self, client: AsyncClient):
"""Unauthenticated request returns 401."""
payload = {"email": "test@example.com", "display_name": "Test"}
response = await client.post("/api/v1/users", json=payload)
assert response.status_code == 401
class TestListUsers:
"""GET /api/v1/users"""
async def test_list_users_success(self, authenticated_client: AsyncClient):
"""Returns a paginated list of users."""
response = await authenticated_client.get("/api/v1/users?limit=10")
assert response.status_code == 200
data = response.json()
assert isinstance(data["items"], list)
assert "next_cursor" in data
assert "has_more" in data
async def test_list_users_pagination(self, authenticated_client: AsyncClient):
"""Pagination returns correct page size."""
response = await authenticated_client.get("/api/v1/users?limit=2")
assert response.status_code == 200
data = response.json()
assert len(data["items"]) <= 2
async def test_list_users_with_cursor(
self, authenticated_client: AsyncClient, sample_user
):
"""Cursor-based pagination returns next page."""
# Get first page
first_page = await authenticated_client.get("/api/v1/users?limit=1")
cursor = first_page.json().get("next_cursor")
if cursor:
second_page = await authenticated_client.get(
f"/api/v1/users?limit=1&cursor={cursor}"
)
assert second_page.status_code == 200
class TestGetUser:
"""GET /api/v1/users/{user_id}"""
async def test_get_user_success(
self, authenticated_client: AsyncClient, sample_user
):
"""Returns the user when they exist."""
response = await authenticated_client.get(f"/api/v1/users/{sample_user.id}")
assert response.status_code == 200
data = response.json()
assert data["id"] == sample_user.id
assert data["email"] == sample_user.email
async def test_get_user_not_found(self, authenticated_client: AsyncClient):
"""Returns 404 for a non-existent user ID."""
response = await authenticated_client.get("/api/v1/users/99999")
assert response.status_code == 404
assert "not found" in response.json()["detail"].lower()
class TestUpdateUser:
"""PATCH /api/v1/users/{user_id}"""
async def test_update_user_success(
self, authenticated_client: AsyncClient, sample_user
):
"""Partial update succeeds with valid data."""
response = await authenticated_client.patch(
f"/api/v1/users/{sample_user.id}",
json={"display_name": "Updated Name"},
)
assert response.status_code == 200
assert response.json()["display_name"] == "Updated Name"
async def test_update_user_not_found(self, authenticated_client: AsyncClient):
"""Updating a non-existent user returns 404."""
response = await authenticated_client.patch(
"/api/v1/users/99999",
json={"display_name": "Ghost"},
)
assert response.status_code == 404
class TestDeleteUser:
"""DELETE /api/v1/users/{user_id}"""
async def test_delete_user_admin_success(
self, admin_client: AsyncClient, sample_user
):
"""Admin can delete a user."""
response = await admin_client.delete(f"/api/v1/users/{sample_user.id}")
assert response.status_code == 204
async def test_delete_user_non_admin_forbidden(
self, authenticated_client: AsyncClient, sample_user
):
"""Non-admin cannot delete a user."""
response = await authenticated_client.delete(
f"/api/v1/users/{sample_user.id}"
)
assert response.status_code == 403
async def test_delete_user_not_found(self, admin_client: AsyncClient):
"""Deleting a non-existent user returns 404."""
response = await admin_client.delete("/api/v1/users/99999")
assert response.status_code == 404
"""
conftest-template.py — Production-grade root conftest for FastAPI + SQLAlchemy async tests.
Place this file at: tests/conftest.py
Provides:
- Async SQLAlchemy engine and session fixtures (SQLite in-memory for speed)
- httpx.AsyncClient fixture wired to the FastAPI app with DB override
- Authentication helper fixtures (standard user, admin user)
- Factory session wiring
Dependencies:
pip install pytest pytest-asyncio httpx sqlalchemy[asyncio] aiosqlite factory-boy
"""
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine,
async_sessionmaker,
)
from app.main import app
from app.database import Base, get_db
from app.auth import create_access_token
# ─── Engine & Session ────────────────────────────────────────────────────────────
@pytest.fixture(scope="session")
def anyio_backend():
"""Select asyncio as the async backend for the entire test session."""
return "asyncio"
@pytest.fixture(scope="session")
async def engine():
"""Create an async engine and initialize all tables once per session."""
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
echo=False,
)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
await engine.dispose()
@pytest.fixture
async def db_session(engine):
"""
Provide a transactional database session that rolls back after each test.
This ensures complete test isolation -- each test starts with a clean state.
"""
async_session = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
async with async_session() as session:
async with session.begin():
yield session
# Rollback any changes made during the test
await session.rollback()
# ─── HTTP Client ─────────────────────────────────────────────────────────────────
@pytest.fixture
async def client(db_session: AsyncSession):
"""
Async HTTP client pointing at the FastAPI app.
The app's database dependency is overridden to use the test session,
so all requests share the same transactional session (and its rollback).
"""
async def _override_get_db():
yield db_session
app.dependency_overrides[get_db] = _override_get_db
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()
# ─── Authentication Helpers ──────────────────────────────────────────────────────
def _make_auth_headers(user_id: int, role: str = "member") -> dict[str, str]:
"""Create Authorization headers with a JWT for the given user."""
token = create_access_token(data={"sub": str(user_id), "role": role})
return {"Authorization": f"Bearer {token}"}
@pytest.fixture
def auth_headers() -> dict[str, str]:
"""Authorization headers for a standard test user (id=1, role=member)."""
return _make_auth_headers(user_id=1, role="member")
@pytest.fixture
def admin_headers() -> dict[str, str]:
"""Authorization headers for an admin user (id=99, role=admin)."""
return _make_auth_headers(user_id=99, role="admin")
@pytest.fixture
async def authenticated_client(client: AsyncClient, auth_headers: dict) -> AsyncClient:
"""AsyncClient pre-configured with standard user auth headers."""
client.headers.update(auth_headers)
return client
@pytest.fixture
async def admin_client(client: AsyncClient, admin_headers: dict) -> AsyncClient:
"""AsyncClient pre-configured with admin auth headers."""
client.headers.update(admin_headers)
return client
# ─── Factory Session Wiring ──────────────────────────────────────────────────────
@pytest.fixture(autouse=True)
def _wire_factories(db_session: AsyncSession):
"""
Automatically set the DB session on all SQLAlchemy-backed factories.
Import your factories here so they use the per-test transactional session.
"""
from tests.factories.user_factory import UserFactory
from tests.factories.order_factory import OrderFactory
UserFactory._meta.sqlalchemy_session = db_session
OrderFactory._meta.sqlalchemy_session = db_session
# ─── Sample Data Fixtures ────────────────────────────────────────────────────────
@pytest.fixture
async def sample_user(db_session: AsyncSession):
"""Create and persist a standard user for tests that need an existing user."""
from tests.factories.user_factory import UserFactory
user = UserFactory()
db_session.add(user)
await db_session.flush()
return user
@pytest.fixture
async def sample_order(db_session: AsyncSession, sample_user):
"""Create and persist an order linked to the sample_user."""
from tests.factories.order_factory import OrderFactory
order = OrderFactory(user_id=sample_user.id)
db_session.add(order)
await db_session.flush()
return order
"""
factory-template.py — factory_boy factories for test data generation.
Place factory files at: tests/factories/
Each factory provides:
- Sensible defaults so tests can create objects with zero arguments
- Overridable fields for specific test scenarios
- Sequence-based IDs to avoid collisions
- Both in-memory (.build()) and DB-persisted (.create()) usage
Dependencies:
pip install factory-boy
"""
import factory
from datetime import datetime, timezone
from app.models import User, Order, OrderItem
# ─── User Factory ────────────────────────────────────────────────────────────────
class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
"""
Factory for creating User model instances.
Usage:
# In-memory (no DB write):
user = UserFactory.build()
# Persisted to DB (requires session wiring in conftest):
user = UserFactory.create()
# Override defaults:
admin = UserFactory.build(role="admin", is_active=True)
# Batch:
users = UserFactory.build_batch(5)
"""
class Meta:
model = User
sqlalchemy_session = None # Set per-test via conftest fixture
sqlalchemy_session_persistence = "commit"
id = factory.Sequence(lambda n: n + 1)
email = factory.LazyAttribute(lambda obj: f"user{obj.id}@example.com")
display_name = factory.Faker("name")
role = "member"
is_active = True
created_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
updated_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
class Params:
"""Traits for common variations."""
admin = factory.Trait(
role="admin",
display_name=factory.LazyAttribute(lambda obj: f"Admin {obj.id}"),
)
inactive = factory.Trait(
is_active=False,
)
# ─── Order Factory ───────────────────────────────────────────────────────────────
class OrderFactory(factory.alchemy.SQLAlchemyModelFactory):
"""
Factory for creating Order model instances.
Usage:
# Basic order (auto-creates a user):
order = OrderFactory.build()
# Order for a specific user:
order = OrderFactory.build(user_id=42)
# Override status:
shipped = OrderFactory.build(status="shipped")
"""
class Meta:
model = Order
sqlalchemy_session = None
sqlalchemy_session_persistence = "commit"
id = factory.Sequence(lambda n: n + 1)
user_id = factory.LazyAttribute(lambda obj: UserFactory.build().id)
status = "pending"
total_cents = factory.Faker("random_int", min=500, max=500000)
currency = "USD"
notes = None
created_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
updated_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
class Params:
"""Traits for common order states."""
completed = factory.Trait(
status="completed",
)
cancelled = factory.Trait(
status="cancelled",
notes="Cancelled by customer",
)
# ─── Order Item Factory ──────────────────────────────────────────────────────────
class OrderItemFactory(factory.alchemy.SQLAlchemyModelFactory):
"""
Factory for creating OrderItem model instances.
Usage:
item = OrderItemFactory.build(order_id=1, product_name="Widget")
"""
class Meta:
model = OrderItem
sqlalchemy_session = None
sqlalchemy_session_persistence = "commit"
id = factory.Sequence(lambda n: n + 1)
order_id = factory.LazyAttribute(lambda obj: OrderFactory.build().id)
product_name = factory.Faker("word")
quantity = factory.Faker("random_int", min=1, max=10)
unit_price_cents = factory.Faker("random_int", min=100, max=50000)
# ─── Usage Examples ──────────────────────────────────────────────────────────────
#
# # Build without persisting (unit tests):
# user = UserFactory.build()
# user = UserFactory.build(role="admin")
# users = UserFactory.build_batch(3)
#
# # Build with trait:
# admin = UserFactory.build(admin=True)
# inactive = UserFactory.build(inactive=True)
#
# # Persisted (integration tests with conftest session wiring):
# user = UserFactory.create()
# order = OrderFactory.create(user_id=user.id)
#
# # Related objects:
# user = UserFactory.create()
# order = OrderFactory.create(user_id=user.id)
# items = OrderItemFactory.create_batch(3, order_id=order.id)
"""
integration-test-template.py — Integration test with real database.
Place at: tests/integration/test_user_integration.py
This template demonstrates:
- Full round-trip testing: service -> repository -> database -> assertions
- Using a real test database (SQLite in-memory via conftest fixtures)
- Testing data persistence, relationships, and query behavior
- No mocks -- every layer executes real code
When to use integration tests vs unit tests:
- Unit tests (service-test-template.py): Fast, mock the repo, test business logic.
- Integration tests (this file): Slower, real DB, test that layers work together.
- Both are needed: unit tests for logic coverage, integration tests for confidence.
"""
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from app.services.user_service import UserService
from app.services.order_service import OrderService
from app.repositories.user_repository import UserRepository
from app.repositories.order_repository import OrderRepository
from app.exceptions import NotFoundError, ConflictError
# ─── Mark the entire module as integration tests ─────────────────────────────────
pytestmark = [pytest.mark.integration]
# ─── Fixtures ────────────────────────────────────────────────────────────────────
@pytest.fixture
def user_repo(db_session: AsyncSession):
"""Real user repository backed by the test database."""
return UserRepository(db_session)
@pytest.fixture
def order_repo(db_session: AsyncSession):
"""Real order repository backed by the test database."""
return OrderRepository(db_session)
@pytest.fixture
def user_service(user_repo):
"""UserService wired to the real repository."""
return UserService(user_repo=user_repo)
@pytest.fixture
def order_service(order_repo, user_repo):
"""OrderService wired to real repositories."""
return OrderService(order_repo=order_repo, user_repo=user_repo)
# ─── User CRUD Integration Tests ─────────────────────────────────────────────────
class TestUserCRUDIntegration:
"""Full round-trip CRUD tests for users with real database."""
async def test_create_and_retrieve_user(self, user_service):
"""Create a user and verify it can be retrieved."""
# Create
created = await user_service.create_user(
email="integration@example.com",
display_name="Integration User",
)
assert created.id is not None
# Retrieve
fetched = await user_service.get_user(created.id)
assert fetched.email == "integration@example.com"
assert fetched.display_name == "Integration User"
assert fetched.is_active is True
async def test_update_user_persists(self, user_service):
"""Update a user and verify changes are persisted."""
# Create
user = await user_service.create_user(
email="update-test@example.com",
display_name="Before Update",
)
# Update
updated = await user_service.update_user(
user_id=user.id,
display_name="After Update",
)
assert updated.display_name == "After Update"
# Re-fetch to confirm persistence
refetched = await user_service.get_user(user.id)
assert refetched.display_name == "After Update"
async def test_delete_user_removes_from_db(self, user_service):
"""Delete a user and verify they are gone."""
user = await user_service.create_user(
email="delete-test@example.com",
display_name="To Delete",
)
await user_service.delete_user(user.id)
with pytest.raises(NotFoundError):
await user_service.get_user(user.id)
async def test_duplicate_email_raises_conflict(self, user_service):
"""Creating two users with the same email raises ConflictError."""
await user_service.create_user(
email="unique@example.com",
display_name="First",
)
with pytest.raises(ConflictError, match="already exists"):
await user_service.create_user(
email="unique@example.com",
display_name="Second",
)
# ─── Relationship Integration Tests ──────────────────────────────────────────────
class TestOrderUserRelationship:
"""Test that orders and users relate correctly in the database."""
async def test_create_order_for_user(self, user_service, order_service):
"""Create an order linked to a real user."""
user = await user_service.create_user(
email="buyer@example.com",
display_name="Buyer",
)
order = await order_service.create_order(
user_id=user.id,
items=[{"product_name": "Widget", "quantity": 2, "unit_price_cents": 1500}],
)
assert order.user_id == user.id
assert order.status == "pending"
assert order.total_cents == 3000 # 2 * 1500
async def test_user_orders_list(self, user_service, order_service):
"""Retrieve all orders belonging to a user."""
user = await user_service.create_user(
email="multi-order@example.com",
display_name="Frequent Buyer",
)
await order_service.create_order(
user_id=user.id,
items=[{"product_name": "A", "quantity": 1, "unit_price_cents": 100}],
)
await order_service.create_order(
user_id=user.id,
items=[{"product_name": "B", "quantity": 1, "unit_price_cents": 200}],
)
orders = await order_service.list_orders_for_user(user.id)
assert len(orders) == 2
async def test_order_for_nonexistent_user_fails(self, order_service):
"""Creating an order for a user that does not exist raises NotFoundError."""
with pytest.raises(NotFoundError, match="not found"):
await order_service.create_order(
user_id=99999,
items=[{"product_name": "X", "quantity": 1, "unit_price_cents": 100}],
)
# ─── Query and Pagination Integration Tests ──────────────────────────────────────
class TestUserQueryIntegration:
"""Test query behavior with real data in the database."""
async def test_list_users_returns_created_users(self, user_service):
"""List endpoint returns users that were created."""
await user_service.create_user(email="list1@example.com", display_name="One")
await user_service.create_user(email="list2@example.com", display_name="Two")
users = await user_service.list_users(limit=10)
emails = [u.email for u in users]
assert "list1@example.com" in emails
assert "list2@example.com" in emails
async def test_list_users_respects_limit(self, user_service):
"""List endpoint respects the limit parameter."""
for i in range(5):
await user_service.create_user(
email=f"limit{i}@example.com",
display_name=f"User {i}",
)
users = await user_service.list_users(limit=2)
assert len(users) <= 2
# ─── Transaction Isolation Verification ───────────────────────────────────────────
class TestTransactionIsolation:
"""Verify that test isolation works -- each test starts with a clean state."""
async def test_first_creates_user(self, user_service):
"""This test creates a user. The next test should NOT see it."""
await user_service.create_user(
email="isolation@example.com",
display_name="Isolated",
)
user = await user_service.get_user_by_email("isolation@example.com")
assert user is not None
async def test_second_does_not_see_first(self, user_service):
"""The user from the previous test should not exist (transaction rolled back)."""
with pytest.raises(NotFoundError):
await user_service.get_user_by_email("isolation@example.com")
"""
service-test-template.py — Annotated service unit test with mocked repository.
Place at: tests/unit/services/test_user_service.py
This template demonstrates:
- Testing service business logic in isolation
- Mocking repositories (the data layer) with AsyncMock
- Mocking external services (email, notifications)
- Testing success paths, error paths, and edge cases
- Using parametrize for input variations
Note on mocking strategy:
- Mock the repository layer (data access) so service tests run without a DB.
- Do NOT mock Pydantic validation or domain logic -- let it execute naturally.
- For integration tests that hit the real DB, see integration-test-template.py.
"""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime, timezone
from app.services.user_service import UserService
from app.models import User
from app.exceptions import NotFoundError, ConflictError, ForbiddenError
# ─── Fixtures ────────────────────────────────────────────────────────────────────
@pytest.fixture
def mock_user_repo():
"""Create a mocked user repository with common async methods."""
repo = AsyncMock()
repo.get_by_id = AsyncMock(return_value=None)
repo.get_by_email = AsyncMock(return_value=None)
repo.create = AsyncMock()
repo.update = AsyncMock()
repo.delete = AsyncMock()
repo.list_all = AsyncMock(return_value=[])
return repo
@pytest.fixture
def user_service(mock_user_repo):
"""Create a UserService with mocked dependencies."""
return UserService(user_repo=mock_user_repo)
@pytest.fixture
def existing_user():
"""A User object representing an existing user in the system."""
return User(
id=1,
email="alice@example.com",
display_name="Alice",
role="member",
is_active=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
# ─── Create User Tests ──────────────────────────────────────────────────────────
class TestCreateUser:
"""Tests for UserService.create_user()."""
async def test_create_user_success(self, user_service, mock_user_repo):
"""Creating a user with a unique email succeeds."""
# Arrange: email does not exist yet
mock_user_repo.get_by_email.return_value = None
mock_user_repo.create.return_value = User(
id=1,
email="new@example.com",
display_name="New User",
role="member",
is_active=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
# Act
result = await user_service.create_user(
email="new@example.com",
display_name="New User",
)
# Assert
assert result.email == "new@example.com"
assert result.display_name == "New User"
assert result.role == "member"
mock_user_repo.create.assert_called_once()
async def test_create_user_duplicate_email_raises(
self, user_service, mock_user_repo, existing_user
):
"""Creating a user with an existing email raises ConflictError."""
# Arrange: email already exists
mock_user_repo.get_by_email.return_value = existing_user
# Act & Assert
with pytest.raises(ConflictError, match="already exists"):
await user_service.create_user(
email="alice@example.com",
display_name="Duplicate",
)
# Verify we never attempted to create
mock_user_repo.create.assert_not_called()
@pytest.mark.parametrize("role", ["member", "admin", "viewer"])
async def test_create_user_with_role(self, user_service, mock_user_repo, role):
"""Users can be created with any valid role."""
mock_user_repo.get_by_email.return_value = None
mock_user_repo.create.return_value = User(
id=1, email="test@example.com", display_name="Test",
role=role, is_active=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
result = await user_service.create_user(
email="test@example.com",
display_name="Test",
role=role,
)
assert result.role == role
# ─── Get User Tests ──────────────────────────────────────────────────────────────
class TestGetUser:
"""Tests for UserService.get_user()."""
async def test_get_user_found(self, user_service, mock_user_repo, existing_user):
"""Returns the user when they exist."""
mock_user_repo.get_by_id.return_value = existing_user
result = await user_service.get_user(user_id=1)
assert result.id == 1
assert result.email == "alice@example.com"
mock_user_repo.get_by_id.assert_called_once_with(1)
async def test_get_user_not_found(self, user_service, mock_user_repo):
"""Raises NotFoundError when user does not exist."""
mock_user_repo.get_by_id.return_value = None
with pytest.raises(NotFoundError, match="not found"):
await user_service.get_user(user_id=999)
# ─── Update User Tests ──────────────────────────────────────────────────────────
class TestUpdateUser:
"""Tests for UserService.update_user()."""
async def test_update_display_name(
self, user_service, mock_user_repo, existing_user
):
"""Updating display_name succeeds."""
mock_user_repo.get_by_id.return_value = existing_user
existing_user.display_name = "Updated Alice"
mock_user_repo.update.return_value = existing_user
result = await user_service.update_user(
user_id=1, display_name="Updated Alice"
)
assert result.display_name == "Updated Alice"
async def test_update_nonexistent_user(self, user_service, mock_user_repo):
"""Updating a non-existent user raises NotFoundError."""
mock_user_repo.get_by_id.return_value = None
with pytest.raises(NotFoundError):
await user_service.update_user(user_id=999, display_name="Ghost")
# ─── Delete User Tests ──────────────────────────────────────────────────────────
class TestDeleteUser:
"""Tests for UserService.delete_user()."""
async def test_delete_user_success(
self, user_service, mock_user_repo, existing_user
):
"""Deleting an existing user succeeds."""
mock_user_repo.get_by_id.return_value = existing_user
await user_service.delete_user(user_id=1)
mock_user_repo.delete.assert_called_once_with(1)
async def test_delete_user_not_found(self, user_service, mock_user_repo):
"""Deleting a non-existent user raises NotFoundError."""
mock_user_repo.get_by_id.return_value = None
with pytest.raises(NotFoundError):
await user_service.delete_user(user_id=999)
# ─── External Service Mocking ───────────────────────────────────────────────────
class TestUserNotifications:
"""Tests for notification side effects during user operations."""
async def test_welcome_email_sent_on_create(self, user_service, mock_user_repo):
"""A welcome email is sent when a new user is created."""
mock_user_repo.get_by_email.return_value = None
mock_user_repo.create.return_value = User(
id=1, email="new@example.com", display_name="New",
role="member", is_active=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
with patch(
"app.services.user_service.EmailClient"
) as mock_email_cls:
mock_email = mock_email_cls.return_value
mock_email.send_welcome = AsyncMock(return_value=True)
await user_service.create_user(
email="new@example.com", display_name="New"
)
mock_email.send_welcome.assert_called_once_with("new@example.com")
async def test_create_user_succeeds_even_if_email_fails(
self, user_service, mock_user_repo
):
"""User creation should not fail if the welcome email fails."""
mock_user_repo.get_by_email.return_value = None
mock_user_repo.create.return_value = User(
id=1, email="new@example.com", display_name="New",
role="member", is_active=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
with patch(
"app.services.user_service.EmailClient"
) as mock_email_cls:
mock_email = mock_email_cls.return_value
mock_email.send_welcome = AsyncMock(
side_effect=Exception("SMTP error")
)
# Should NOT raise despite email failure
result = await user_service.create_user(
email="new@example.com", display_name="New"
)
assert result.email == "new@example.com"
#!/usr/bin/env bash
# check-test-coverage.sh — Run pytest with coverage and fail if below threshold.
#
# Usage:
# ./check-test-coverage.sh [--output-dir <dir>] [--fail-under <pct>]
#
# Options:
# --output-dir <dir> Directory to write coverage results (default: ./coverage-results)
# --fail-under <pct> Minimum coverage percentage (default: 80)
set -euo pipefail
# ─── Defaults ───────────────────────────────────────────────────────────────────
OUTPUT_DIR="./coverage-results"
FAIL_UNDER=80
# ─── Parse arguments ────────────────────────────────────────────────────────────
while [[ $# -gt 0 ]]; do
case "$1" in
--output-dir)
OUTPUT_DIR="$2"
shift 2
;;
--fail-under)
FAIL_UNDER="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [--output-dir <dir>] [--fail-under <pct>]"
exit 0
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
# ─── Setup ───────────────────────────────────────────────────────────────────────
mkdir -p "$OUTPUT_DIR"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
RESULTS_FILE="$OUTPUT_DIR/coverage-report-${TIMESTAMP}.txt"
HTML_DIR="$OUTPUT_DIR/htmlcov"
echo "=== Test Coverage Check ==="
echo "Fail-under threshold: ${FAIL_UNDER}%"
echo "Output directory: ${OUTPUT_DIR}"
echo ""
# ─── Run pytest with coverage ────────────────────────────────────────────────────
EXIT_CODE=0
pytest \
--cov=app \
--cov-report=term-missing \
--cov-report="html:${HTML_DIR}" \
--cov-report="json:${OUTPUT_DIR}/coverage.json" \
--cov-fail-under="${FAIL_UNDER}" \
-q \
2>&1 | tee "$RESULTS_FILE" || EXIT_CODE=$?
echo "" >> "$RESULTS_FILE"
echo "Timestamp: $(date -Iseconds)" >> "$RESULTS_FILE"
echo "Threshold: ${FAIL_UNDER}%" >> "$RESULTS_FILE"
# ─── Report result ───────────────────────────────────────────────────────────────
if [[ $EXIT_CODE -eq 0 ]]; then
echo ""
echo "PASS: Coverage meets the ${FAIL_UNDER}% threshold."
echo "Status: PASS" >> "$RESULTS_FILE"
echo "HTML report: ${HTML_DIR}/index.html"
echo "Full report: ${RESULTS_FILE}"
else
echo ""
echo "FAIL: Coverage is below the ${FAIL_UNDER}% threshold."
echo "Status: FAIL" >> "$RESULTS_FILE"
echo "Review missing coverage in: ${HTML_DIR}/index.html"
echo "Full report: ${RESULTS_FILE}"
fi
exit $EXIT_CODE