Bird
0
0

Identify the error in this test code:

medium📝 Debug Q6 of 15
Django - Testing Django Applications
Identify the error in this test code:
class Car(models.Model):
    model = models.CharField(max_length=50)

class CarTest(TestCase):
    def test_car_creation(self):
        car = Car(model='Tesla')
        self.assertEqual(car.model, 'Tesla')
ATest method name does not start with 'test_'
BCar object is not saved before assertion
CModel field is missing max_length
DTestCase class is not imported
Step-by-Step Solution
Solution:
  1. Step 1: Check object creation and saving

    The Car object is created but not saved to the test database.
  2. Step 2: Understand assertion context

    Even without saving, accessing car.model works, so test passes but does not test DB save.
  3. Step 3: Identify best practice

    To fully test model creation, car.save() should be called before assertion.
  4. Final Answer:

    Car object is not saved before assertion -> Option B
  5. Quick Check:

    Save object to test DB before assertions [OK]
Quick Trick: Call save() to test database persistence [OK]
Common Mistakes:
MISTAKES
  • Assuming object is saved automatically on creation
  • Ignoring save() before assertions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes