factory_boy: A Python Test Fixtures Replacement
This repository profile is provided by osrepos.com, an open source repository discovery platform.

Summary
factory_boy is a powerful Python library designed to replace static, hard-to-maintain test fixtures with dynamic, easy-to-use factories. It simplifies the creation of complex objects for testing, offering features like declarative syntax, ORM integration, and realistic data generation. This tool helps developers write cleaner, more maintainable tests across various Python projects.
Repository Information
Topics
Click on any tag to explore related repositories
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Introduction
factory_boy is a popular Python library that serves as a robust replacement for traditional test fixtures. Inspired by thoughtbot's factory_bot, it aims to simplify the creation of complex objects for testing purposes. Instead of relying on static, often hard-to-maintain fixtures, factory_boy allows you to define flexible factories that generate customized objects on demand, making your test setups cleaner and more adaptable. It integrates seamlessly with various ORMs like Django, SQLAlchemy, and MongoEngine, and supports features such as declarative syntax, multiple build strategies, and realistic data generation.
Installation
Installing factory_boy is straightforward using pip:
pip install factory_boy
For development or to install from source, you can clone the repository:
git clone git://github.com/FactoryBoy/factory_boy/
python setup.py install
Examples
factory_boy provides a powerful and flexible way to define and use test data.
Defining Factories
Factories declare attributes used to instantiate a Python object. The model class is specified within a Meta class:
import factory
from . import models
class UserFactory(factory.Factory):
class Meta:
model = models.User
first_name = 'John'
last_name = 'Doe'
admin = False
# Another factory for the same object
class AdminFactory(factory.Factory):
class Meta:
model = models.User
first_name = 'Admin'
last_name = 'User'
admin = True
ORM Integrationfactory_boy offers specific factory subclasses for popular ORMs:
- Django:
factory.django.DjangoModelFactory - Mogo:
factory.mogo.MogoFactory - MongoEngine:
factory.mongoengine.MongoEngineFactory - SQLAlchemy:
factory.alchemy.SQLAlchemyModelFactory
Using Factories
You can instantiate objects using different strategies:
# Returns a User instance that's not saved
user = UserFactory.build()
# Returns a saved User instance (requires an ORM base class)
user = UserFactory.create()
# Returns a stub object (just attributes)
obj = UserFactory.stub()
# The Factory class acts as a shortcut for the default strategy (create)
user = UserFactory()
# Override attributes by passing keyword arguments
user = UserFactory.build(first_name='Joe')
# Create multiple objects in a single call
users = UserFactory.build_batch(10, first_name="Joe")
Realistic, Random Values with Faker
Integrate with the faker library to generate realistic, random data for your tests:
class RandomUserFactory(factory.Factory):
class Meta:
model = models.User
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
Lazy Attributes
For attributes whose values depend on other fields or require dynamic computation, use LazyAttribute or LazyFunction:
import factory
from datetime import datetime
class UserFactory(factory.Factory):
class Meta:
model = models.User
first_name = 'Joe'
last_name = 'Blow'
email = factory.LazyAttribute(lambda a: '{}.{}@example.com'.format(a.first_name, a.last_name).lower())
date_joined = factory.LazyFunction(datetime.now)
Sequences
Generate unique, sequential values for fields like email addresses:
class UserFactory(factory.Factory):
class Meta:
model = models.User
email = factory.Sequence(lambda n: 'person{}@example.com'.format(n))
Associations
Define relationships between objects using SubFactory:
class PostFactory(factory.Factory):
class Meta:
model = models.Post
author = factory.SubFactory(UserFactory)
Why Use It
factory_boy significantly enhances test development by:
- Simplifying Test Setup: Reduces the boilerplate code needed to set up complex test data.
- Improving Readability: Factories clearly define how objects are constructed, making tests easier to understand.
- Increasing Maintainability: Centralizes object creation logic, so changes to models only require updates in one place.
- Flexibility: Supports various ORMs and allows for dynamic attribute generation, including realistic data.
- Reproducibility: Offers tools to reseed random values, ensuring tests remain consistent.
Links
- Documentation: https://factoryboy.readthedocs.io/
- Repository: https://github.com/FactoryBoy/factory_boy
- Package (PyPI): https://pypi.org/project/factory-boy/
- Mailing List: factoryboy@googlegroups.com | https://groups.google.com/forum/#!forum/factoryboy
Related repositories
Similar repositories that may be relevant next.

MarkLLM: An Open-Source Toolkit for LLM Watermarking
June 23, 2026
MarkLLM is an open-source toolkit designed to simplify the research and application of watermarking technologies for large language models (LLMs). It offers a unified framework for implementing various watermarking algorithms, alongside robust visualization and comprehensive evaluation tools. This toolkit helps researchers and the broader community understand and assess the authenticity and origin of machine-generated text.

Agent-Reach: Empower Your AI Agents with Internet Access, Zero API Fees
June 21, 2026
Agent-Reach is a powerful GitHub repository that equips AI agents with the ability to access and search the entire internet, including platforms like Twitter, Reddit, YouTube, and Bilibili. It provides a streamlined CLI experience, eliminating the need for complex API configurations and associated fees. This project ensures your AI agent can "see" and interact with web content effortlessly.
REAL Video Enhancer: AI-Powered Video Interpolation, Upscaling, and Denoising
June 19, 2026
REAL Video Enhancer is a powerful open-source application designed to enhance video quality across Linux, Windows, and macOS. It leverages AI models for advanced video processing tasks such as frame interpolation, upscaling, decompression, and denoising. This tool provides a modern alternative to older software, making high-quality video enhancement accessible to a wider audience.

NVIDIA PhysicsNeMo: Deep Learning Framework for Physics-ML Models
June 16, 2026
NVIDIA PhysicsNeMo is an open-source deep learning framework designed for building, training, and fine-tuning Physics AI models. It leverages state-of-the-art scientific machine learning methods, enabling real-time predictions by combining physics knowledge with data. This framework provides scalable, GPU-optimized tools for AI4Science and engineering applications.
Source repository
Open the original repository on GitHub.