factory-boy
A versatile test fixtures replacement based on thoughtbot's factory_bot for Ruby.
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()
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 3.3.3 | Imported from PyPI (3.3.3) | Low | 4/21/2026 |
| v2.9.0 | Tag v2.9.0 | Low | 7/30/2017 |
| v2.8.1 | Tag v2.8.1 | Low | 12/17/2016 |
| v2.8.0 | Tag v2.8.0 | Low | 12/17/2016 |
| v2.7.0 | Tag v2.7.0 | Low | 4/19/2016 |
| v2.6.1 | Tag v2.6.1 | Low | 2/10/2016 |
| v2.6.0 | Tag v2.6.0 | Low | 10/20/2015 |
| v2.5.2 | Tag v2.5.2 | Low | 4/21/2015 |
| v2.5.1 | Tag v2.5.1 | Low | 3/27/2015 |
| v2.5.0 | Tag v2.5.0 | Low | 3/26/2015 |
| v2.4.1 | Tag v2.4.1 | Low | 6/23/2014 |
| v2.4.0 | Tag v2.4.0 | Low | 6/21/2014 |
| v2.3.1 | Tag v2.3.1 | Low | 1/22/2014 |
| v2.3.0 | Tag v2.3.0 | Low | 12/25/2013 |
| v2.2.1 | Tag v2.2.1 | Low | 9/25/2013 |
| v2.2.0 | Tag v2.2.0 | Low | 9/24/2013 |
| v2.1.2 | Tag v2.1.2 | Low | 8/14/2013 |
| v2.1.1 | Tag v2.1.1 | Low | 7/2/2013 |
| v2.1.0 | Tag v2.1.0 | Low | 6/26/2013 |
| v2.0.2 | Tag v2.0.2 | Low | 4/16/2013 |
| v2.0.1 | Tag v2.0.1 | Low | 4/16/2013 |
