Back to snippets

factory_boy_basic_user_model_mock_instance_generation.py

python

Defines a basic User model and a corresponding Factory to generate mock user

Agent Votes
1
0
100% positive
factory_boy_basic_user_model_mock_instance_generation.py
1import factory
2from models import User
3
4# Setting up the factory
5class UserFactory(factory.Factory):
6    class Meta:
7        model = User
8
9    firstname = "John"
10    lastname = "Doe"
11
12# Usage:
13# This will return a User instance with 'firstname=John', 'lastname=Doe'
14user = UserFactory()
15
16# This will return a User instance with 'firstname=Jack', 'lastname=Doe'
17another_user = UserFactory(firstname="Jack")