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.

text-to-cad: AI Agent Skills for CAD, CAE, and CAM Workflows
September 24, 2026
text-to-cad is a comprehensive Python library developed by earthtojake, providing a rich set of agent skills for Computer-Aided Design (CAD), Computer-Aided Engineering (CAE), and Computer-Aided Manufacturing (CAM). It enables AI agents to generate, inspect, source, slice, and manage CAD and robot-description artifacts, streamlining complex engineering processes.

dcc-mcp-blender: AI-Driven 3D Workflows with an Embedded MCP Server
September 24, 2026
dcc-mcp-blender is a powerful Blender addon that integrates an embedded Streamable HTTP MCP server directly into Blender. This allows any MCP-compatible AI client to seamlessly control and automate your 3D modeling, animation, and rendering workflows. It offers over 200 pre-built tools and an extensible skill system for robust production environments.

i-have-adhd: Streamline Your Coding Agent's Output for Clarity
September 23, 2026
i-have-adhd is a powerful skill designed to transform your coding assistant's responses into clear, actionable, and concise outputs. It prevents AI agents from burying essential information, ensuring you receive direct answers and numbered steps without unnecessary preamble or fluff. This tool is ideal for developers seeking enhanced productivity and straightforward guidance from their AI coding partners.
Guaardvark: Your Self-Hosted AI Studio for Agents, Media, and Code
September 21, 2026
Guaardvark is a comprehensive, self-hosted AI studio designed for local execution of advanced AI tasks. It integrates coding agents, media generation (video, image, music, voice), and robust RAG capabilities, all running on a single GPU. This platform prioritizes privacy and user control, enabling a full AI workstation experience on your own hardware.
Source repository
Open the original repository on GitHub.
13 counted GitHub visits