Q&A
Managing test data in pytest — factory pattern, fixtures, or external files?
Ajitesh MohantaAmbassador
Apr 29, 2026 951 0
As our API test suite grows I'm realising ad-hoc test data in each test function is a maintenance nightmare. Three options I'm weighing:
1. **factory-boy** — model factories that generate realistic data, great for DB tests
2. **pytest fixtures** — explicit, composable, but can get verbose for complex objects
3. **JSON/YAML files** — easy to read, hard to vary programmatically
We test a FastAPI + PostgreSQL stack, so we need data both at the API level (request payloads) and DB level (pre-seeded records).
What's your setup and what would you change if starting fresh?