What does it do?
When testing an application, you often need sample database records, like a fake user, a fake order, a fake product, but manually creating them for every single test gets repetitive fast. Factory Boy lets you define a factory, a reusable blueprint describing what a typical record looks like, and then generate as many variations of it as you want with one line of code, optionally overriding just the specific fields a given test cares about. It’s directly inspired by a similar tool from Ruby called factory_bot, adapted for Python’s testing conventions.
See it in action
This defines a reusable blueprint for creating fake ‘User’ records with a realistic name and email, then uses it to stamp out one example user.
import factory
from myapp.models import User
class UserFactory(factory.Factory):
class Meta:
model = User
name = factory.Faker("name")
email = factory.Faker("email")
user = UserFactory()
print(user.name, user.email)
Why would a non-developer care?
Tests that share tangled, copy-pasted setup code become a maintenance nightmare the moment the underlying data model changes. Factory Boy centralizes that setup logic in one place, so a single blueprint update fixes every test that depends on it instead of hunting through dozens of files.
Real-world examples
It’s especially popular in the Django web framework community, where test suites often need realistic, related database objects, like a user with an order with line items, set up correctly and consistently across hundreds of tests. Without a tool like it, teams tend to end up with duplicated setup code scattered everywhere, so a single change to a database model can quietly break tests in a dozen unrelated files.
Who uses it
Developers, especially in the Django ecosystem, who need consistent, reusable ways to generate test database records.
How it compares to alternatives
It’s often paired with Faker, which supplies the realistic fake values that fill in a factory’s fields, and it’s a close Python counterpart to Ruby’s factory_bot, which directly inspired its design.
Fun fact
Factory Boy’s name and design are a direct homage to Ruby’s factory_bot, originally named factory_girl, ported over for Django and Python’s testing world.