0
0
Djangoframework~20 mins

Testing models in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Model Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
ATest passes successfully
BTest fails with AssertionError
CRaises AttributeError because price is missing
DRaises TypeError due to wrong argument
Attempts:
2 left
💡 Hint
Check what the __str__ method of the Product model returns.
state_output
intermediate
2: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()
A2
B0
C1
DRaises DatabaseError
Attempts:
2 left
💡 Hint
Count how many Product objects are created in the test.
🔧 Debug
advanced
2: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)
ANo error, object created
BValueError
CTypeError
Ddjango.db.utils.IntegrityError
Attempts:
2 left
💡 Hint
Check if the name field is required and what happens if it is missing.
📝 Syntax
advanced
2: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)
A
def test_product(self):
self.assertTrue(True)
B
def test_product(self):
    self.assertTrue(True)
C
def test_product():
    self.assertTrue(True)
D
def test_product(self):
    assertTrue(True)
Attempts:
2 left
💡 Hint
Check for missing colon and indentation.
🧠 Conceptual
expert
2:00remaining
Which option best describes Django's test database behavior?
When running Django model tests, what happens to the test database after tests complete?
AThe test database is created once and reused for all test runs without deletion
BThe test database is the same as the development database and is not deleted
CThe test database is created before tests and destroyed after tests finish
DThe test database is created after tests finish
Attempts:
2 left
💡 Hint
Think about how Django isolates tests from your real data.