factory_boy
Python test fixture replacement library. factory_boy provides factories for creating test objects with realistic data. Instead of manually constructing test objects in every test, define a Factory class once and generate objects with random or specified data. Integrates with Django ORM, SQLAlchemy, and Mongoengine. Replaces fragile fixtures with programmatic, flexible object generation.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Testing library — no network access. Test data should never contain real PII. Faker generates fake but realistic-looking data.
⚡ Reliability
Best When
You have Django or SQLAlchemy models and want to generate realistic test objects with relationships without writing repetitive fixture setup code.
Avoid When
Your tests don't involve ORM models or you need extremely high-volume test data generation — raw factories have per-object overhead.
Use Cases
- • Generate realistic test data for agent data model tests without manually creating every field
- • Create test fixtures for database-backed agent tests with SQLAlchemy or Django ORM integration
- • Build parameterized test factories for agent input validation tests with controlled variation
- • Use SubFactory to generate related objects in agent integration tests with proper relationship setup
- • Combine with Faker for realistic-looking test data (names, emails, addresses) in agent test suites
Not For
- • Non-ORM test data — for plain Python objects without ORM, dataclasses or pytest fixtures may be simpler
- • Production data generation — factory_boy is a testing tool, not for seeding production databases
- • Performance testing with millions of records — factories have overhead; use bulk_create or CSV imports for large datasets
Interface
Authentication
Local testing library — no authentication.
Pricing
Completely free and open source.
Agent Metadata
Known Gotchas
- ⚠ create() vs build() vs stub() — create() hits the database, build() creates Python objects only; using create() in unit tests that don't need DB access slows tests significantly
- ⚠ Sequence values are global by default — parallel test execution (pytest-xdist) can cause sequence collisions; use factory.reset_sequence() or unique constraints
- ⚠ SubFactory creates parent objects every time — if creating 100 child objects, 100 parent objects are also created unless explicitly reused with a single parent instance
- ⚠ Lazy attributes (LazyAttribute, LazyFunction) are evaluated per instance — referencing other fields in LazyAttribute requires using the obj parameter correctly
- ⚠ Django's test database must be active before factory.create() is called — factories in pytest conftest setup may execute before the test database is initialized
- ⚠ Factory inheritance works but Meta.model must be set on each concrete factory — abstract factories without Meta.model raise CyclicDefinitionError
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for factory_boy.
Scores are editorial opinions as of 2026-03-06.