Django 6.1.3

13.4 Commands, checks, and testing

Creating, cloning, and tearing down test databases

How does DiscoverRunner spin up isolated test databases, serialize initial state, and clone databases across --parallel workers?

Typing "python manage.py test" invokes Django’s test runner — DiscoverRunner. Before a single test_* method executes, DiscoverRunner.run_tests orchestrates environment patching, database discovery, schema migration, optional JSON state serialization, parallel database cloning, and system checks.

Skipping unused databases and BaseDatabaseCreation.create_test_db

Open DiscoverRunner.run_tests and DiscoverRunner._get_databases:

  1. setup_test_environment(): Switches "EMAIL_BACKEND" to in-memory django.core.mail.backends.locmem.EmailBackend (populating "mail.outbox"), forces DEBUG = False, installs template instrumentation (instrumented_test_render) so Client can record rendered templates and contexts, and disables ALLOWED_HOSTS errors for "testserver".
  2. Smart database discovery (_get_databases): Before touching any database, DiscoverRunner walks every test case in the discovered unittest.TestSuite inspecting two class attributes: "databases" (empty {} on SimpleTestCase, {"default"} on TestCase and TransactionTestCase) and "serialized_rollback". If your test run contains only SimpleTestCase classes, DiscoverRunner creates ZERO test databases!
  3. Creating & migrating (BaseDatabaseCreation.create_test_db, BaseDatabaseCreation.create_test_db): For every needed non-mirror alias, Django computes the isolated database name ("test_<NAME>" on PostgreSQL/MySQL, or ":memory:" / shared-cache "file:memorydb_default?mode=memory&cache=shared" on SQLite), creates the empty DB (or reuses it under "--keepdb"), rewrites "settings.DATABASES[alias]['NAME']" in place, and runs call_command("migrate", verbosity=..., interactive=False, run_syncdb=True) (skipping migration files only if "TEST['MIGRATE'] = False"). Notice DiscoverRunner.run_checks(databases) runs after test DB creation so Tags.database checks can validate the migrated schema!

serialize_db_to_string and --parallel cloning via clone_test_db

Two subtle mechanisms keep multi-test and multi-core runs both accurate and fast:

Watch DiscoverRunner._get_databases distinguish SimpleTestCase, TestCase, and serialized_rollback=True TransactionTestCase — and watch serialize_db_to_string() snapshot "desk/"'s migrated database (15,830 JSON chars) live:

Recorded on Django 6.1.3 ch13-tooling script
4. DiscoverRunner database discovery and test DB serialization
  runner_databases: SimpleTestCase={} TestCase={'default': False} serialized_TransactionTestCase={'default': True}
  serialize_db_to_string: json_chars=15830 contains_story_model=True

Now that the test database is migrated and ready, why can TestCase execute 15 database-writing tests in 8.62 ms while TransactionTestCase takes 178.03 ms — and why does django.db.transaction.on_commit behave completely differently inside TestCase? We open the exact transaction layers next in Why TestCase is fast and when TransactionTestCase is required.