Bird
0
0

Given this factory definition:

medium📝 component behavior Q13 of 15
Django - Testing Django Applications
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?
ANone, because username is not set
BThe literal string 'user_name'
CAn error because Faker is not imported
DA random username string generated by Faker
Step-by-Step Solution
Solution:
  1. Step 1: Understand Faker usage in Factory Boy

    Using factory.Faker('user_name') generates a random username string each time the factory is called.
  2. Step 2: Evaluate the expression UserFactory().username

    Calling UserFactory() creates a User instance with a random username, so .username returns that random string.
  3. Final Answer:

    A random username string generated by Faker -> Option D
  4. Quick Check:

    Faker('user_name') = random username string [OK]
Quick Trick: Faker fields produce random data, not literals [OK]
Common Mistakes:
MISTAKES
  • Thinking Faker returns the field name as string
  • Assuming missing imports cause runtime error here
  • Expecting None if not explicitly set

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes