DEV.co
Open-Source Testing · FactoryBoy

factory_boy

factory_boy is a Python library that replaces static test fixtures with declarative factories for creating test objects. It simplifies test setup by allowing you to define only the fields relevant to each test while auto-generating defaults, and integrates with Django, SQLAlchemy, MongoDB, and other ORMs.

Source: GitHub — github.com/FactoryBoy/factory_boy
3.8k
GitHub stars
418
Forks
Python
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
RepositoryFactoryBoy/factory_boy
OwnerFactoryBoy
Primary languagePython
LicenseMIT — OSI-approved
Stars3.8k
Forks418
Open issues208
Latest releaseUnknown
Last updated2026-01-01
Sourcehttps://github.com/FactoryBoy/factory_boy

What factory_boy is

A fixture replacement tool providing multiple build strategies (build, create, stub), lazy attribute evaluation, sequences, SubFactory associations, and Faker integration for realistic test data. Supports Django, SQLAlchemy, MongoEngine, and Mogo ORM backends via inheritance-based factory classes.

Quickstart

Get the factory_boy source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/FactoryBoy/factory_boy.gitcd factory_boy# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Django/SQLAlchemy test suites

Streamlines complex ORM object creation in test cases, reducing boilerplate while maintaining readable, maintainable test specs. Scales well from unit tests to integration tests.

Parameterized/data-driven testing

Supports batch creation and attribute overrides, enabling concise generation of object variations for edge-case testing without repetitive fixture code.

Fixture migration and cleanup

Ideal for teams moving away from static JSON/YAML fixtures; provides cleaner syntax and better version control due to factory code living in Python rather than separate data files.

Implementation considerations

  • Requires Python environment and pip; integrates as a dev dependency—no production impact.
  • Learn factory syntax and declarative patterns; documentation includes examples for Django, SQLAlchemy, and plain models.
  • Plan ORM integration upfront (DjangoModelFactory, SQLAlchemyModelFactory, etc.) to avoid refactoring factories later.
  • Use Faker judiciously; seeding with factory.random.reseed_random() is needed for reproducible test runs.
  • Consider factory inheritance and mixins for DRY factory definitions across many models.

When to avoid it — and what to weigh

  • Non-ORM or custom object models — While factory_boy supports plain Python classes, its design philosophy favors ORM integration. Projects with bespoke object initialization may require significant custom coding.
  • High-volume performance testing — Factory creation has runtime overhead; if tests require millions of objects or sub-millisecond creation time, direct instantiation or bulk SQL inserts may be more suitable.
  • Real-time production data synthesis — factory_boy is a testing library. Using it for production data generation or ETL is out of scope and not recommended.
  • Projects already deeply invested in fixtures — Migration cost from existing fixture frameworks (e.g., pytest fixtures, unittest fixtures) may outweigh benefits if test coverage is large and stable.

License & commercial use

Licensed under MIT (Massachusetts Institute of Technology License), an OSI-approved permissive license allowing commercial use, modification, and distribution with minimal restrictions. Attribution required; no warranty provided.

MIT license permits commercial use in proprietary projects, products, and services. No license fees or restrictions on commercial deployment. Typical for test libraries. Recommend including MIT license text in distributions as per standard practice.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceModerate
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

factory_boy is a testing library executed in dev/test environments. No production exposure. Standard Python dependency security hygiene applies: pin versions, audit for CVEs via tools like pip-audit or Dependabot. Use Faker with caution if test data is intended to resemble PII; mask before sharing test artifacts. No encryption, authentication, or secrets management handled by factory_boy.

Alternatives to consider

pytest fixtures

Built-in pytest feature for parametrized test data; lighter-weight for simple cases but less expressive for complex object graphs and ORM associations.

Hypothesis (property-based testing)

Generates randomized test data to find edge cases; complementary rather than replacement, and does not handle ORM object setup as directly.

django-seed / model_bakery

Django-specific alternatives; model_bakery is lighter-weight and auto-discovers fields, but less explicit and less portable across ORMs.

Software development agency

Build on factory_boy with DEV.co software developers

Evaluate factory_boy for your Django, SQLAlchemy, or ORM-based project. Compare against pytest fixtures and model_bakery. MIT license, no production risk.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

factory_boy FAQ

Does factory_boy work with non-ORM objects?
Yes. The base factory.Factory class works with any Python class. ORM integration (Django, SQLAlchemy, etc.) is optional and provided via subclasses like DjangoModelFactory.
Can I use factory_boy with pytest?
Yes. factory_boy is test-runner agnostic. Import factories directly in pytest test functions or conftest.py fixtures.
How do I ensure test reproducibility with random data?
Call factory.random.reseed_random(seed_string) in test setup. This ensures Faker and sequence counters produce consistent output across runs.
What is the performance impact on large test suites?
Factory instantiation has overhead compared to direct object construction. For high-volume tests, profile and consider batch strategies or direct instantiation; factory_boy is designed for clarity, not performance.

Software developers & web developers for hire

From first prototype to production, DEV.co delivers software development services around tools like factory_boy. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source testing and beyond.

Ready to streamline your Python test suite?

Evaluate factory_boy for your Django, SQLAlchemy, or ORM-based project. Compare against pytest fixtures and model_bakery. MIT license, no production risk.