0
0
Djangoframework~5 mins

Factory Boy for test data in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
How do you define a related model inside a Factory Boy factory?
AUsing SubFactory
BUsing ForeignKeyField
CUsing RelatedFactory
DUsing RelatedModel
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
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
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.