← Back to Django Course | Chapter 13: Testing in Django | Lesson 5 of 9

Testing Models

Testing a model means checking that your data blueprint actually saves, reads back, and behaves the way you designed it.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Testing against the real production database instead of Django's automatic throwaway test database.
  2. Forgetting to call full_clean() when a test needs to check field validation, since .save() alone skips validators.
  3. Only testing that an object saves, without checking that its __str__ or computed properties return the right value.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.