Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is Factory Boy used for in Django testing?
Factory Boy helps create test data easily by defining blueprints (factories) for your models. It saves time and keeps tests clean.
Click to reveal answer
beginner
How do you define a simple Factory Boy factory for a Django model?
You create a class inheriting from <code>factory.django.DjangoModelFactory</code> and set the model inside a <code>class Meta</code>. Then define fields with default values.
Click to reveal answer
beginner
What method do you call to create and save an instance using Factory Boy?
Use FactoryClass.create() to make and save an instance to the database.
Click to reveal answer
intermediate
How can Factory Boy help with related models in Django?
You can use SubFactory to create related model instances automatically when creating the main model instance.
Click to reveal answer
beginner
Why is Factory Boy better than manually creating test data in Django tests?
Factory Boy reduces repetitive code, makes test data consistent, and improves test readability and maintenance.
Click to reveal answer
Which class should a Factory Boy factory inherit from for Django models?
Afactory.ModelFactory
Bfactory.Factory
Cdjango.test.TestCase
Dfactory.django.DjangoModelFactory
✗ Incorrect
For Django models, factories inherit from factory.django.DjangoModelFactory to integrate with Django ORM.
What does the create() method do in Factory Boy?
ACreates and saves an instance to the database
BOnly creates an instance without saving
CDeletes a test instance
DUpdates an existing instance
✗ Incorrect
create() makes a new instance and saves it to the database.
How do you define a related model inside a Factory Boy factory?
AUsing SubFactory
BUsing ForeignKeyField
CUsing RelatedFactory
DUsing RelatedModel
✗ Incorrect
SubFactory is used to create related model instances automatically.
Which of these is NOT a benefit of using Factory Boy?
AImproves test readability
BReduces repetitive test data code
CAutomatically runs tests
DKeeps test data consistent
✗ Incorrect
Factory Boy helps with test data but does not run tests automatically.
Where do you specify the Django model inside a Factory Boy factory?
AAs a class attribute named model_class
BInside the Meta class as model
CIn the factory's __init__ method
DIn the factory's create method
✗ Incorrect
The Django model is set inside a nested Meta class as the model attribute.
Explain how to create a Factory Boy factory for a Django model and use it to generate test data.
Think about the class structure and how to call the factory.
You got /4 concepts.
Describe how Factory Boy handles related models when creating test data in Django.
Focus on how factories connect to other factories.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using Factory Boy in Django testing?
easy
A. To create reusable fake data for tests easily
B. To speed up the Django server
C. To replace Django's ORM
D. To deploy Django applications automatically
Solution
Step 1: Understand Factory Boy's role
Factory Boy is designed to generate fake data for tests, making test setup easier and less repetitive.
Step 2: Eliminate unrelated options
Speeding up the server, replacing ORM, or deployment are unrelated to test data creation.
Final Answer:
To create reusable fake data for tests easily -> Option A
Quick Check:
Factory Boy = reusable fake test data [OK]
Hint: Factory Boy = fake test data creator [OK]
Common Mistakes:
Thinking Factory Boy speeds up the server
Confusing Factory Boy with deployment tools
Assuming Factory Boy replaces Django ORM
2. Which of the following is the correct way to define a basic factory for a Django model Book using Factory Boy?
easy
A. class BookFactory(factory.DjangoModelFactory): class Meta: model = Book
B. class BookFactory(factory.Factory): model = Book
C. class BookFactory(factory.ModelFactory): model = Book
D. class BookFactory(factory.DjangoFactory): class Meta: model = Book
Solution
Step 1: Identify correct base class
Factory Boy uses DjangoModelFactory as the base class for Django models.
Step 2: Check Meta class usage
The model must be specified inside a nested Meta class with attribute model.
Final Answer:
class BookFactory(factory.DjangoModelFactory): class Meta: model = Book -> Option A
Hint: Use DjangoModelFactory with Meta.model for Django models [OK]
Common Mistakes:
Using factory.Factory instead of DjangoModelFactory
Not using a Meta class for model assignment
Using incorrect base class names
3. Given this factory definition:
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
username = factory.Faker('user_name')
email = factory.Faker('email')
What will UserFactory().username return?
medium
A. None, because username is not set
B. The literal string 'user_name'
C. An error because Faker is not imported
D. A random username string generated by Faker
Solution
Step 1: Understand Faker usage in Factory Boy
Using factory.Faker('user_name') generates a random username string each time the factory is called.
Step 2: Evaluate the expression UserFactory().username
Calling UserFactory() creates a User instance with a random username, so .username returns that random string.
Final Answer:
A random username string generated by Faker -> Option D
Quick Check:
Faker('user_name') = random username string [OK]
Hint: Faker fields produce random data, not literals [OK]
Common Mistakes:
Thinking Faker returns the field name as string
Assuming missing imports cause runtime error here
Expecting None if not explicitly set
4. What is wrong with this factory code?
class ProductFactory(factory.DjangoModelFactory):
class Meta:
model = Product
name = factory.Faker('product_name')
price = factory.Faker('float')
medium
A. Faker does not have a 'product_name' provider
B. The 'float' provider requires arguments to specify range
C. Missing import of factory module
D. Meta class should be outside the factory class
Solution
Step 1: Check Faker providers used
Faker has no built-in 'product_name' provider, but this is a common custom name; however, 'float' requires arguments like min and max.
Step 2: Identify the error cause
Using factory.Faker('float') without arguments causes an error because Faker's float provider needs parameters.
Final Answer:
The 'float' provider requires arguments to specify range -> Option B
Quick Check:
Faker float needs min/max args [OK]
Hint: Faker float needs range arguments to work [OK]
Common Mistakes:
Assuming 'product_name' is always valid
Ignoring required arguments for Faker float
Thinking Meta class placement is wrong
5. You want to create a factory for a Django model Order that has a foreign key to User. How do you correctly define the user field in OrderFactory to use UserFactory?
hard
A. user = UserFactory()
B. user = factory.RelatedFactory(UserFactory)
C. user = factory.SubFactory(UserFactory)
D. user = factory.Faker('user')
Solution
Step 1: Understand foreign key factory usage
To link a foreign key to another factory, use factory.SubFactory with the related factory class.
Step 2: Evaluate options
Directly calling UserFactory() assigns an instance at class load time, not per object. RelatedFactory is for reverse relations. Faker does not create model instances.
Final Answer:
user = factory.SubFactory(UserFactory) -> Option C
Quick Check:
Foreign key uses SubFactory [OK]
Hint: Use SubFactory for foreign key relations [OK]