Test Fixtures
This document details the standard fixtures used in testing.
Common Fixtures (tests/conftest.py)
client
Async HTTP client for testing API endpoints.
- Type:
httpx.AsyncClient - Scope: Function
db_session
Async database session for database operations.
- Type:
sqlalchemy.ext.asyncio.AsyncSession - Scope: Function
- Behavior: Per-test session for isolation.
auth_headers
Authenticated request headers built via a real login flow.
- Type:
dict[str, str](often includesAuthorization: Bearer <token>) - Note: The reference backend’s fixture also returns the authenticated
user_idalongside headers for convenience.
Module-Specific Fixtures
Create fixtures in tests/{module}/fixtures.py or conftest.py inside the module folder.
Pattern for Entity Fixtures
@pytest.fixture
async def sample_item(db_session: AsyncSession, tenant_id: str):
"""Create a sample item for testing."""
item = Item(
id="test-item-id",
tenant_id=tenant_id,
name="Test Item",
price=100
)
db_session.add(item)
await db_session.commit()
await db_session.refresh(item)
yield item
# Cleanup (if not handled by db fixture rollback)
await db_session.delete(item)
await db_session.commit()
Scope Rules
- Session Scope: Expensive setup (e.g., creating database engine).
- Function Scope: DB sessions, test data (ensures isolation).