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
E-commerce (paypal) integration with Django Ashwin Kumar

E-commerce is integration is becoming almost essential for every web application now a days. There are so many payment gateways to integrate with our application. ...

Continue Reading...
Setting Up Coveralls for Django Project Ravi Kumar Gadila

Coveraslls will check the code coverage for your test cases. To use coveralls.io your code must be hosted on GitHub or BitBucket.

install coveralls
...

Continue Reading...
django Payu Payment gateway Integration Shirisha Gaddi

In this blog, we will see how to integrate Django and PayU Payment Gateway. To integrate with PayU, we have package called "django-payu" - a ...

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