Django 6.1.3

Part IV · In practice

Chapter 13: Commands, checks, and testing

The promiseTests run in a clean copy of your app.

The machineryHow manage.py discovers commands, how system checks guard startup, why the autoreloader spawns a child process, and how TestCase uses transaction rollbacks instead of recreating tables.

Part IV steps outside the HTTP request handler into the terminal workflow every Django engineer lives in all day: manage.py, system checks, the development server’s autoreloader, and Django’s test suite. Because running "manage.py test" finishes in milliseconds and leaves the database untouched between test methods, it feels like every test runs inside a freshly cloned copy of your application.

Open the source and the illusion dissolves into four clever pieces of engineering — each with sharp boundaries you need to know when a test passes locally and fails in production. ManagementUtility discovers commands in reverse "INSTALLED_APPS" order and guards BaseCommand.execute with CheckRegistry, whose "--deploy" security checks never run during normal development. run_with_reloader spawns a parent supervisor and child worker (invoking AppConfig.ready twice!). TestCase achieves a 20x speedup over TransactionTestCase by wrapping each class in an uncommitted outer transaction and rolling back an inner savepoint after every test — which is why django.db.transaction.on_commit callbacks never fire unless captured explicitly! And ClientHandler runs your full WSGI middleware stack with zero sockets while quietly disabling CSRF enforcement by default.

Why TestCase is 20x faster than TransactionTestCase

Figure 13.1 Every timing bar and isolation fact is parsed directly from ch13-tooling and ch13-bench. Rolling back an inner savepoint takes microseconds compared to flushing every table and re-seeding fixtures after every test (8.62 ms vs. 178.03 ms over 15 DB tests, 20.65x faster) — and SimpleTestCase skips database setup altogether (0.42 ms, 429x faster)!

The developer terminal loop in six stops

First we trace execute_from_command_line into BaseCommand, then separate static checks from "--deploy" and "database" checks inside CheckRegistry. Next we open StatReloader and restart_with_reloader to explain why "runserver" runs two processes.

Then we dissect Django’s entire test stack: DiscoverRunner and BaseDatabaseCreation database creation and cloning, the exact nested transaction mechanics separating SimpleTestCase, TestCase, and TransactionTestCase, and how Client and RequestFactory differ from production WSGIHandler.

We start at the command line in manage.py, discovery, and BaseCommand.