Bird
0
0

Identify the error in this Django model test code:

medium📝 Debug Q14 of 15
Django - Testing Django Applications
Identify the error in this Django model test code:
class UserProfile(models.Model):
    age = models.IntegerField()

class UserProfileTest(TestCase):
    def test_age_positive(self):
        profile = UserProfile(age=-5)
        self.assertTrue(profile.age > 0)
AThe model field type is incorrect for age
BThe test method name does not start with 'test_'
CThe test will fail because age is negative but no validation is done
DThe test should use assertFalse instead of assertTrue
Step-by-Step Solution
Solution:
  1. Step 1: Review the test logic

    The test creates a UserProfile with age -5 and asserts age > 0, which is false.
  2. Step 2: Identify why the test fails

    There is no validation preventing negative age, so the test fails as expected.
  3. Final Answer:

    The test will fail because age is negative but no validation is done -> Option C
  4. Quick Check:

    Negative age without validation causes test failure [OK]
Quick Trick: Check test logic matches model validation to avoid failure [OK]
Common Mistakes:
MISTAKES
  • Assuming test method name is wrong
  • Thinking IntegerField rejects negatives by default
  • Confusing assertTrue with assertFalse usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes