Factory Boy - An alternative for Fixtures

Reading Time : ~ .

Factory Boy is a fixtures replacement tool. It allows you to use objects customized for the current test, while only declaring the test-specific fields.

Setting up your factories:

For testing the Django app, create a root directory named tests. Then create __init__.py, tests.py (to write your tests), factories.py (to write your model factories) in that directory.

So, your app layout looks like:

  myapp
    |- __init__.py
    |- admin.py
    |- forms.py
    |- models.py
    |- tests
        |- __init__.py
        |- factories.py
        |- tests.py
    |- urls.py
    |- views.py

Features of Factory Boy:

It allows you to automate much of the testing scenarios. Some examples are:

  1. Use a 'Sequence' object using a lambda function to dynamically create unique field values:
        EX: username = factory.Sequence(lambda n: 'user_%d' % n)

  2. Use a LazyAttribute object to pick from a range of choices for a defined field
        EX: choice = factory.LazyAttribute(lambda s: random.choice(dict(MyModel.CHOICES).keys()))

A basic factory: Factories declare a set of attributes used to instantiate an object. Here's a basic factory that generates auth.User instances:

# factories.py
from factory import DjangoModelFactory, lazy_attribute
from django.contrib.auth.models import User

class UserFactory(DjangoModelFactory):
    first_name = 'Shirisha'
    last_name = 'Gaddi'
    username = factory.Sequence(lambda n: 'user_%d' % n)
    email = lazy_attribute(lambda obj: obj.username + "@example.com")

    class Meta:
        model = 'User'
        django_get_or_create = ('username',)

Calling this as factories.UserFactory() will perform same task as User.objects.create call. Howerver, it is possible to override the defined attributes by passing keyword arguments:

# Create a user instance by overriding first_name
user = factories.UserFactory(first_name="Karuna")

The factory's Meta has django_get_or_create set, which means the factory will call the Django built-in User.objects.get_or_create method.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Working with Django Plugins Vinisha Naladala

This blog describes about how to work with django-plugins. Django Plugin is a Simple Plugin Framework for Django. By using django-plugins, you can make your ...

Continue Reading...
Improving page speed score in Google Page Score test- PART2 Chaitanya Kattineni

In our previous blog post we got an idea of what are the things that Google's Page Speed Insights will take into count to give ...

Continue Reading...
Integrate Django-Oscar-Accounts with Django-Oscar Shirisha Gaddi

This package uses double-entry bookkeeping where every transaction is recorded twice (once for the source and once for the destination). This ensures the books always ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties