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

Test Database Behavior

Django builds a brand-new, temporary practice database just for your tests, so your real data is never touched or broken.

Why a separate test database exists

If tests wrote to your real database, they could corrupt real data or leave behind junk rows. Django solves this by building a throwaway database before the suite starts.

Example: Why a separate test database exists

If tests wrote to your real database, they could corrupt real data or leave behind junk rows. Django solves this by building a throwaway database before the suite starts.

bash
python manage.py test

⚠️ Run this command in your terminal.

Automatic rollback per test

TestCase wraps each test method in a transaction and rolls it back afterward, so objects created in one test never leak into the next.

Example: Automatic rollback per test

TestCase wraps each test method in a transaction and rolls it back afterward, so objects created in one test never leak into the next.

markup
from django.test import TestCase
from myapp.models import Book


class IsolationTests(TestCase):
    def test_one_creates_a_book(self):
        Book.objects.create(title='Temp Book')
        self.assertEqual(Book.objects.count(), 1)

    def test_two_starts_with_no_books(self):
        self.assertEqual(Book.objects.count(), 0)
{# 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. #}

SQLite and the test database

With SQLite as DATABASES[default], Django's test runner can create the test database in memory by default, making the whole suite run quickly with zero extra setup.

Example: SQLite and the test database

With SQLite as DATABASES[default], Django's test runner can create the test database in memory by default, making the whole suite run quickly with zero extra setup.

markup
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
{# 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. Worrying that running tests will modify or delete real production/development data — Django's test database is entirely separate and thrown away afterward.
  2. Assuming data created in one test method is visible in another — each test method's database changes are rolled back automatically.
  3. Not realizing the test database uses the same engine as DATABASES[default] (SQLite here), so no extra configuration is needed for a SQLite project.
Chapter Summary
  • Before running tests, Django creates a separate test database, by default named with a test_ prefix.
  • Each individual test method runs inside a database transaction that is rolled back when the test finishes.
  • After the full suite finishes, Django destroys the test database automatically.
  • For SQLite projects, the test database can even run entirely in memory, making tests fast.

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.