How I structure conftest.py files across a large monorepo — the pattern that works
Ajitesh MohantaAmbassador
May 10, 2026 1,911 0
After three rewrites, here's the conftest.py structure that finally works for our monorepo (4 services, ~800 tests):
```
tests/
conftest.py ← global: env vars, logging, session DB
fixtures/
auth.py ← auth tokens, user factories
db.py ← database session, transaction rollback
http.py ← httpx client, base URLs
services/
billing/
conftest.py ← billing-specific fixtures only
inventory/
conftest.py ← inventory-specific fixtures only
```
**Key rules we enforce:**
1. Global conftest only has fixtures needed by 3+ services
2. Each service conftest imports from `tests/fixtures/` — never from sibling service conftests
3. Fixtures that create data always clean up via `yield` + teardown
4. No business logic in fixtures — they set up state, not assert on it
Before this: 1 root conftest with 60 fixtures nobody understood. After: onboarding takes hours, not days.