Challenge - 5 Problems
Django Model Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django model test?
Consider this Django model test code. What will be the output when running this test?
Django
from django.test import TestCase from myapp.models import Product class ProductTest(TestCase): def test_str_method(self): product = Product(name='Book', price=10) self.assertEqual(str(product), 'Book')
Attempts:
2 left
💡 Hint
Check what the __str__ method of the Product model returns.
✗ Incorrect
The test creates a Product instance with name 'Book' and price 10. The test checks if str(product) returns 'Book'. If the Product model's __str__ method returns the name, the test passes.
❓ state_output
intermediate2:00remaining
What is the value of product_count after test?
Given this Django test case, what is the value of product_count after running test_product_creation?
Django
from django.test import TestCase from myapp.models import Product class ProductTest(TestCase): def test_product_creation(self): Product.objects.create(name='Pen', price=5) Product.objects.create(name='Pencil', price=3) product_count = Product.objects.count()
Attempts:
2 left
💡 Hint
Count how many Product objects are created in the test.
✗ Incorrect
Two Product objects are created in the test, so the count is 2.
🔧 Debug
advanced2:00remaining
What error does this Django model test raise?
This test tries to create a Product without a required field. What error will it raise?
Django
from django.test import TestCase from myapp.models import Product class ProductTest(TestCase): def test_missing_name(self): Product.objects.create(price=10)
Attempts:
2 left
💡 Hint
Check if the name field is required and what happens if it is missing.
✗ Incorrect
If the name field is required (not nullable), the database will raise IntegrityError when trying to insert a null value.
📝 Syntax
advanced2:00remaining
Which option fixes the syntax error in this Django test?
Identify the correct syntax to define a Django test method inside a TestCase class.
Django
from django.test import TestCase class ProductTest(TestCase): def test_product(self) self.assertTrue(True)
Attempts:
2 left
💡 Hint
Check for missing colon and indentation.
✗ Incorrect
Option B correctly adds the missing colon and proper indentation for the method body.
🧠 Conceptual
expert2:00remaining
Which option best describes Django's test database behavior?
When running Django model tests, what happens to the test database after tests complete?
Attempts:
2 left
💡 Hint
Think about how Django isolates tests from your real data.
✗ Incorrect
Django creates a separate test database before tests run and deletes it after tests finish to keep tests isolated.