Challenge - 5 Problems
Factory Boy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Factory Boy factory produce?
Given the following factory definition, what will be the value of
user.email after user = UserFactory() is executed?Django
import factory from myapp.models import User class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User username = factory.Sequence(lambda n: f'user{n}') email = factory.LazyAttribute(lambda obj: f'{obj.username}@example.com') user = UserFactory()
Attempts:
2 left
💡 Hint
Look at how the username is generated and how email uses it.
✗ Incorrect
The username is generated using factory.Sequence starting at 0, so the first user has username user0. The email uses LazyAttribute to build the email from the username, so it becomes user0@example.com.
📝 Syntax
intermediate2:00remaining
Which factory definition has a syntax error?
Identify the factory definition that will cause a syntax error when run.
Attempts:
2 left
💡 Hint
Check the commas between arguments in function calls.
✗ Incorrect
Option A is missing a comma between the string and the keyword argument in factory.Faker('random_number' digits=3), causing a syntax error.
❓ state_output
advanced2:00remaining
What is the value of
order.total after creation?Given this factory and model, what will
order.total be after order = OrderFactory()?Django
import factory from myapp.models import Order class OrderFactory(factory.django.DjangoModelFactory): class Meta: model = Order quantity = 3 price_per_item = 10 total = factory.LazyAttribute(lambda o: o.quantity * o.price_per_item) order = OrderFactory()
Attempts:
2 left
💡 Hint
Look at how total is calculated using LazyAttribute.
✗ Incorrect
The total field uses LazyAttribute to multiply quantity (3) by price_per_item (10), resulting in 30.
🔧 Debug
advanced2:00remaining
Why does this factory raise a RecursionError?
Examine the factory below. Why does creating an instance with
profile = ProfileFactory() cause a RecursionError?Django
import factory from myapp.models import Profile, User class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User username = factory.Sequence(lambda n: f'user{n}') class ProfileFactory(factory.django.DjangoModelFactory): class Meta: model = Profile user = factory.SubFactory(UserFactory) friend = factory.SubFactory('myapp.factories.ProfileFactory') profile = ProfileFactory()
Attempts:
2 left
💡 Hint
Look at the friend field in ProfileFactory.
✗ Incorrect
The friend field uses SubFactory referencing ProfileFactory itself, causing infinite recursion when creating a profile.
🧠 Conceptual
expert3:00remaining
Which option correctly uses Factory Boy to create related objects with different attributes?
You want to create a
Book with two Author objects, each having different names. Which factory setup achieves this correctly?Django
from myapp.models import Book, Author import factory class AuthorFactory(factory.django.DjangoModelFactory): class Meta: model = Author name = factory.Sequence(lambda n: f'Author {n}') class BookFactory(factory.django.DjangoModelFactory): class Meta: model = Book title = 'My Book' authors = factory.RelatedFactoryList(AuthorFactory, related_name='book', size=2)
Attempts:
2 left
💡 Hint
Think about how to create multiple related objects with unique attributes.
✗ Incorrect
Option B uses RelatedFactoryList with size=2 to create two authors. The Sequence in AuthorFactory ensures different names. This is the correct pattern.