# factory-boy

> A versatile test fixtures replacement based on thoughtbot's factory_bot for Ruby.

- **URL**: https://www.freshcrate.ai/projects/factory-boy
- **Author**: Mark Sandstrom
- **Category**: Frameworks
- **Latest version**: `3.3.3` (2026-04-21)
- **License**: MIT
- **Source**: https://github.com/FactoryBoy/factory_boy
- **Language**: Python
- **GitHub**: 3,787 stars, 415 forks
- **Registry**: pypi (`factory-boy`)
- **Tags**: `factory`, `factory_boy`, `fixtures`, `pypi`

## Description

factory_boy
===========

.. image:: https://github.com/FactoryBoy/factory_boy/workflows/Test/badge.svg
    :target: https://github.com/FactoryBoy/factory_boy/actions?query=workflow%3ATest

.. image:: https://github.com/FactoryBoy/factory_boy/workflows/Check/badge.svg
    :target: https://github.com/FactoryBoy/factory_boy/actions?query=workflow%3ACheck

.. image:: https://img.shields.io/pypi/v/factory_boy.svg
    :target: https://factoryboy.readthedocs.io/en/latest/changelog.html
    :alt: Latest Version

.. image:: https://img.shields.io/pypi/pyversions/factory_boy.svg
    :target: https://pypi.org/project/factory-boy/
    :alt: Supported Python versions

.. image:: https://img.shields.io/pypi/wheel/factory_boy.svg
    :target: https://pypi.org/project/factory-boy/
    :alt: Wheel status

.. image:: https://img.shields.io/pypi/l/factory_boy.svg
    :target: https://github.com/FactoryBoy/factory_boy/blob/master/LICENSE
    :alt: License

factory_boy is a fixtures replacement based on thoughtbot's `factory_bot <https://github.com/thoughtbot/factory_bot>`_.

As a fixtures replacement tool, it aims to replace static, hard to maintain fixtures
with easy-to-use factories for complex objects.

Instead of building an exhaustive test setup with every possible combination of corner cases,
``factory_boy`` allows you to use objects customized for the current test,
while only declaring the test-specific fields:

.. code-block:: python

    class FooTests(unittest.TestCase):

        def test_with_factory_boy(self):
            # We need a 200€, paid order, shipping to australia, for a VIP customer
            order = OrderFactory(
                amount=200,
                status='PAID',
                customer__is_vip=True,
                address__country='AU',
            )
            # Run the tests here

        def test_without_factory_boy(self):
            address = Address(
                street="42 fubar street",
                zipcode="42Z42",
                city="Sydney",
                country="AU",
            )
            customer = Customer(
                first_name="John",
                last_name="Doe",
                phone="+1234",
                email="john.doe@example.org",
                active=True,
                is_vip=True,
                address=address,
            )
            # etc.

factory_boy is designed to work well with various ORMs (Django, MongoDB, SQLAlchemy),
and can easily be extended for other libraries.

Its main features include:

- Straightforward declarative syntax
- Chaining factory calls while retaining the global context
- Support for multiple build strategies (saved/unsaved instances, stubbed objects)
- Multiple factories per class support, including inheritance


Links
-----

* Documentation: https://factoryboy.readthedocs.io/
* Repository: https://github.com/FactoryBoy/factory_boy
* Package: https://pypi.org/project/factory-boy/
* Mailing-list: `factoryboy@googlegroups.com <mailto:factoryboy@googlegroups.com>`_ | https://groups.google.com/forum/#!forum/factoryboy


Download
--------

PyPI: https://pypi.org/project/factory-boy/

.. code-block:: sh

    $ pip install factory_boy

Source: https://github.com/FactoryBoy/factory_boy/

.. code-block:: sh

    $ git clone git://github.com/FactoryBoy/factory_boy/
    $ python setup.py install


Usage
-----


.. note:: This section provides a quick summary of factory_boy features.
          A more detailed listing is available in the full documentation.


Defining factories
""""""""""""""""""

Factories declare a set of attributes used to instantiate a Python object.
The class of the object must be defined in the ``model`` field of a ``class Meta:`` attribute:

.. code-block:: python

    import factory
    from . import models

    class UserFactory(factory.Factory):
        class Meta:
            model = models.User

        first_name = 'John'
        last_name = 'Doe'
        admin = False

    # Another, different, factory for the same object
    class AdminFactory(factory.Factory):
        class Meta:
            model = models.User

        first_name = 'Admin'
        last_name = 'User'
        admin = True


ORM integration
"""""""""""""""

factory_boy integration with Object Relational Mapping (ORM) tools is provided
through specific ``factory.Factory`` subclasses:

* Django, with ``factory.django.DjangoModelFactory``
* Mogo, with ``factory.mogo.MogoFactory``
* MongoEngine, with ``factory.mongoengine.MongoEngineFactory``
* SQLAlchemy, with ``factory.alchemy.SQLAlchemyModelFactory``

More details can be found in the ORM section.


Using factories
"""""""""""""""

factory_boy supports several different instantiation strategies: build, create, and stub:

.. code-block:: python

    # Returns a User instance that's not saved
    user = UserFactory.build()

    # Returns a saved User instance.
    # UserFactory must subclass an ORM base class, such as DjangoModelFactory.
    user = UserFactory.create()

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `3.3.3` | 2026-04-21 | Low | Imported from PyPI (3.3.3) |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |
| `master@2026-01-01` | 2026-01-01 | Low | Latest activity on master branch |

## Citation

- HTML: https://www.freshcrate.ai/projects/factory-boy
- Markdown: https://www.freshcrate.ai/projects/factory-boy.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/factory-boy/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
