Testing Models
A simple model to test
Assume a Book model with a title and a CharField. Model tests create real rows in the test database and check their saved values.
Example: A simple model to test
Assume a Book model with a title and a CharField. Model tests create real rows in the test database and check their saved values.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Testing object creation
Creating a model instance inside a test writes to the isolated test database, and the saved fields can be asserted directly.
Example: Testing object creation
Creating a model instance inside a test writes to the isolated test database, and the saved fields can be asserted directly.
from django.test import TestCase
from myapp.models import Book
class BookModelTests(TestCase):
def test_book_title_saved(self):
book = Book.objects.create(title='Django Basics')
self.assertEqual(book.title, 'Django Basics')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Testing the __str__ method
Since __str__ controls how an object appears in the admin and shell, it deserves its own explicit assertion.
Example: Testing the __str__ method
Since __str__ controls how an object appears in the admin and shell, it deserves its own explicit assertion.
class BookStrTests(TestCase):
def test_book_str_returns_title(self):
book = Book.objects.create(title='Django Basics')
self.assertEqual(str(book), 'Django Basics')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Testing against the real production database instead of Django's automatic throwaway test database.
- Forgetting to call full_clean() when a test needs to check field validation, since .save() alone skips validators.
- Only testing that an object saves, without checking that its __str__ or computed properties return the right value.
- Model tests create objects in the test database and assert on their saved fields.
- TestCase automatically gives every test a clean, isolated database state.
- Test both valid data (object saves correctly) and invalid data (validation fails as expected).
- Assert on __str__ output to catch regressions in how objects are displayed.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: