Recall & Review
beginner
What is the main purpose of testing models in Django?
Testing models ensures that the data structure, validations, and business logic in your Django models work correctly before deploying your app.
Click to reveal answer
beginner
Which Django class is commonly used to write tests for models?You use
django.test.TestCase to write tests for models. It provides a test database and useful assertions.Click to reveal answer
beginner
How do you create a test method inside a Django TestCase?
Define a method inside your TestCase class that starts with <code>test_</code>. For example, <code>def test_model_str(self):</code>.Click to reveal answer
intermediate
Why is it important to use the test database when testing Django models?
The test database isolates tests from your real data. It is created fresh for tests and destroyed after, so tests don’t affect your live data.
Click to reveal answer
beginner
What is a simple example of testing a model's string representation in Django?
Create an instance of the model in a test, then use
self.assertEqual(str(instance), 'expected string') to check if the __str__ method works as expected.Click to reveal answer
Which method name prefix is required for Django test methods to run automatically?
✗ Incorrect
Django runs only methods starting with
test_ inside TestCase classes as tests.What does
django.test.TestCase provide for model testing?✗ Incorrect
TestCase sets up a test database and offers helpful assertions for testing models.Why should you avoid using your production database when running model tests?
✗ Incorrect
Tests can create, update, or delete data, so using a test database prevents damage to real data.
Which of these is a good assertion to check a model's field value in a test?
✗ Incorrect
Use
assertEqual to check if a model field matches the expected value.What happens to the test database after Django tests finish?
✗ Incorrect
Django deletes the test database after tests to keep your environment clean.
Explain how to write a simple test for a Django model's string representation.
Think about how you check what str(instance) returns.
You got /4 concepts.
Why is using a test database important when testing Django models?
Consider what could happen if tests ran on your live data.
You got /4 concepts.