Generate realistic users, orders, or addresses for automated tests without writing repetitive setup code.
Override only the fields that matter for each test, keeping test code short and focused.
Create database-backed objects in Django, SQLAlchemy, or MongoEngine with a single factory call.
Reproduce flaky tests caused by random data by locking in a fixed seed for consistent results.
Install via pip install factory_boy. Works out of the box with Django, SQLAlchemy, and MongoEngine. Add faker package separately for realistic data generation.
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.
← factoryboy on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.