0
0
Djangoframework~10 mins

Factory Boy for test data in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Factory Boy for test data
Define Factory Class
Set Attributes & Defaults
Call Factory to Create Instance
Factory Builds Model Instance
Instance Ready for Test Use
Factory Boy creates test data by defining factory classes with default attributes, then builds model instances when called.
Execution Sample
Django
import factory
from myapp.models import User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User
    username = factory.Faker('user_name')

user = UserFactory()
Defines a UserFactory to create User model instances with fake usernames for tests.
Execution Table
StepActionEvaluationResult
1Define UserFactory classClass created with Meta model UserFactory ready to build User instances
2Set username attributeUses Faker to generate usernameusername attribute set to fake value
3Call UserFactory()Triggers factory buildNew User instance created with fake username
4User instance returnedInstance has username attributeInstance ready for test use
5EndNo more actionsFactory usage complete
💡 Factory call completes after creating and returning the model instance
Variable Tracker
VariableStartAfter Step 3Final
UserFactoryClass definedCallable factoryCallable factory
userUndefinedUser instance with fake usernameUser instance with fake username
Key Moments - 2 Insights
Why does calling UserFactory() create a User instance instead of just returning the factory?
Calling UserFactory() triggers the factory's build process, which creates and returns a new User model instance as shown in execution_table step 3.
How does the username get a fake value automatically?
The username attribute uses factory.Faker('user_name'), so when the factory builds the instance, Faker generates a realistic fake username (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 3?
AA new User instance with a fake username is created
BThe factory class is deleted
CNo instance is created yet
DThe username attribute is empty
💡 Hint
Check the 'Result' column in step 3 of the execution_table
At which step does the factory set the username attribute using Faker?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns in step 2 of the execution_table
If you call UserFactory() twice, what changes in variable_tracker for 'user'?
AIt remains the same instance
BIt becomes undefined
CIt holds a new User instance each time
DIt throws an error
💡 Hint
Factory creates a new instance on each call, so variable 'user' changes to a new instance
Concept Snapshot
Factory Boy creates test data by defining factory classes linked to models.
Set default or fake attributes inside the factory.
Calling the factory builds and returns a model instance.
Use Faker for realistic fake data.
Great for clean, reusable test data setup.
Full Transcript
Factory Boy helps create test data in Django by defining factory classes for models. You write a factory class with a Meta inner class pointing to your model. Inside, you set attributes, often using Faker to generate fake data. When you call the factory, it builds and returns a new model instance with those attributes. This lets you quickly create realistic test data without manually writing it each time. The execution flow starts with defining the factory, setting attributes, calling the factory to build, and finally getting the instance ready for tests.