Test Database Behavior
In this page:
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.
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.
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.
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. #}
- Worrying that running tests will modify or delete real production/development data — Django's test database is entirely separate and thrown away afterward.
- Assuming data created in one test method is visible in another — each test method's database changes are rolled back automatically.
- Not realizing the test database uses the same engine as DATABASES[default] (SQLite here), so no extra configuration is needed for a SQLite project.
- 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.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: