explaingit

factoryboy/factory_boy

3,791PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

factory_boy is a Python library for generating test data via factory classes. Define object templates once, then create realistic instances on the fly with automatic defaults and easy field overrides, no messy fixture files needed.

Mindmap

mindmap
  root((factory_boy))
    Core Concepts
      Factory classes
      Default values
      Field overrides
    Database Support
      Django ORM
      SQLAlchemy
      MongoEngine
    Data Generation
      Faker integration
      Lazy attributes
      Sequences
    Test Helpers
      Batch creation
      In-memory stubs
      Fixed random seed
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Generate realistic users, orders, or addresses for automated tests without writing repetitive setup code.

USE CASE 2

Override only the fields that matter for each test, keeping test code short and focused.

USE CASE 3

Create database-backed objects in Django, SQLAlchemy, or MongoEngine with a single factory call.

USE CASE 4

Reproduce flaky tests caused by random data by locking in a fixed seed for consistent results.

Tech stack

PythonDjangoSQLAlchemyMongoEngineFakerPyPI

Getting it running

Difficulty · easy Time to first run · 30min

Install via pip install factory_boy. Works out of the box with Django, SQLAlchemy, and MongoEngine. Add faker package separately for realistic data generation.

Not mentioned in the explanation.

In plain English

factory_boy is a Python library that helps developers generate test data without the pain of writing it all by hand. In software testing, you often need example objects like users, orders, or addresses to run your tests against. The traditional way to do this is with static fixture files, but those files get messy and hard to update as your data model changes. factory_boy lets you define templates for those objects in code, and then create them on the fly with only the specific fields each test actually cares about. The core idea is that you write a factory class that describes a typical instance of your model. When a test needs an object, it calls the factory, which fills in all the default values automatically. If a particular test needs something specific, like a paid order or a VIP customer, you pass in just those fields as overrides and the rest stay as defaults. This keeps individual tests short and focused rather than buried in setup boilerplate. factory_boy integrates directly with the most popular Python database libraries. Django, SQLAlchemy, and MongoEngine all have dedicated support, which means the library knows how to save objects to a database or just build them in memory depending on what you need. You can create a single object, a batch of objects, or just a stub with attributes but no real class behind it. For realistic-looking test data, the library works with a separate package called faker, which generates plausible names, email addresses, and similar fields rather than generic placeholder strings. When randomized data causes a test to break, you can set a fixed random seed so the same data is generated every time, making failures reproducible and easier to diagnose. The library also handles computed fields through lazy attributes, where one field's value is calculated from another, and sequences, which generate unique values like incrementing email addresses. It is installable from PyPI with a single pip command.

Copy-paste prompts

Prompt 1
How do I define a factory for my Django User model and override just the email field in a specific test?
Prompt 2
How can I use factory_boy with Faker to generate realistic names and email addresses instead of placeholder strings?
Prompt 3
What is the difference between build, create, and stub strategies in factory_boy, and when should I use each?
Prompt 4
How do I set up a lazy attribute in factory_boy so one field is computed from another field's value?
Prompt 5
How do I generate a batch of 50 test objects at once using factory_boy?
Open on GitHub → Explain another repo

← factoryboy on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.