Django Test Parallelization
Parallel test execution is a multiplier after a suite is isolated. Treat failures under parallel execution as evidence of hidden shared state until proven otherwise.
Readiness Workflow
Measure serial runtime first.
- If the suite is already dominated by startup or database creation, parallelism may not help enough on its own.
Check isolation before enabling parallelism as the default.
- Run in reverse order.
- Run with randomized order when reverse order is not enough.
- Preserve random seeds so failures can be reproduced.
Enable the runner deliberately.
- Django runner: install
tblib, then run python manage.py test --parallel.
- pytest-django: install
pytest-xdist, then run pytest -n auto.
Classify failures.
- Order-dependent failures usually come from mutated globals, class attributes, settings, caches, monkeypatches, or database assumptions.
- Parallel-only failures usually come from shared resources such as files, cache keys, ports, queues, external services, or temporary directories.
Make resources worker-safe.
- Prefer sharding per process or worker.
- Use locks only when the resource cannot be partitioned.
- Keep lock scope narrow.
Balance work after it is correct.
- Split large test classes or modules only when measurement shows one group holds back the parallel run.
Commands
python -m pip install tblib
python manage.py test --parallel
python manage.py test --parallel 1
python manage.py test --reverse
python -m pip install pytest-xdist
pytest -n auto
pytest -n auto --dist loadscope
For shared-resource patterns, read shared-resources.md.
Decision Rules
- Add parallel testing early in new projects; older serial suites often have hidden isolation assumptions.
- Use CI reverse-order testing as a low-friction guard against order dependencies.
- Use random order when reverse order misses a suspected dependency, and capture the seed.
- Prefer
pytest -n auto --dist loadscope for Django TestCase-heavy suites because class/module setup can be expensive.
- Shard resources before reaching for locks.
- Put process-specific behavior in test settings only, not production settings.
- Split large test groups only after profiling shows an imbalance.
Common Mistakes
- Assuming database transaction isolation protects caches, files, task queues, or external systems.
- Mutating class attributes, globals, app settings, or cache entries without restoring them.
- Enabling random order without preserving the seed.
- Using a single lockfile for too much work and accidentally serializing the suite.
- Letting pytest-xdist split related tests so finely that setup cost is duplicated.
- Making parallelism the default before the suite passes reliably under order-isolation checks.
Verification
Before calling parallelization complete:
- Serial run passes.
- Reverse or random-order run passes, or known failures are fixed.
- Parallel run passes at least twice.
- Any shared-resource sharding lives in test configuration.
- CI has a serial fallback command for debugging.
1---2name: django-test-parallelization3description: Safely parallelize Django and pytest-django test suites with Django --parallel, pytest-xdist, test isolation checks, randomized order detection, shared-resource sharding, locks, and load balancing. Use when enabling parallel tests, diagnosing failures under parallel execution, handling flaky Django tests, or making caches, files, queues, and external resources safe across workers.4license: MIT5compatibility: Codex, Claude Code, and other Agent Skills-compatible clients.6metadata:7 version: "0.1.0"8 displayName: Django Test Parallelization9 category: Django10 tags: django,testing,parallelization,pytest,xdist11---1213# Django Test Parallelization1415Parallel test execution is a multiplier after a suite is isolated. Treat failures under parallel execution as evidence of hidden shared state until proven otherwise.1617## Readiness Workflow18191. Measure serial runtime first.20 - If the suite is already dominated by startup or database creation, parallelism may not help enough on its own.21222. Check isolation before enabling parallelism as the default.23 - Run in reverse order.24 - Run with randomized order when reverse order is not enough.25 - Preserve random seeds so failures can be reproduced.26273. Enable the runner deliberately.28 - Django runner: install `tblib`, then run `python manage.py test --parallel`.29 - pytest-django: install `pytest-xdist`, then run `pytest -n auto`.30314. Classify failures.32 - Order-dependent failures usually come from mutated globals, class attributes, settings, caches, monkeypatches, or database assumptions.33 - Parallel-only failures usually come from shared resources such as files, cache keys, ports, queues, external services, or temporary directories.34355. Make resources worker-safe.36 - Prefer sharding per process or worker.37 - Use locks only when the resource cannot be partitioned.38 - Keep lock scope narrow.39406. Balance work after it is correct.41 - Split large test classes or modules only when measurement shows one group holds back the parallel run.4243## Commands4445```bash46python -m pip install tblib47python manage.py test --parallel48python manage.py test --parallel 149python manage.py test --reverse5051python -m pip install pytest-xdist52pytest -n auto53pytest -n auto --dist loadscope54```5556For shared-resource patterns, read [shared-resources.md](references/shared-resources.md).5758## Decision Rules5960- Add parallel testing early in new projects; older serial suites often have hidden isolation assumptions.61- Use CI reverse-order testing as a low-friction guard against order dependencies.62- Use random order when reverse order misses a suspected dependency, and capture the seed.63- Prefer `pytest -n auto --dist loadscope` for Django `TestCase`-heavy suites because class/module setup can be expensive.64- Shard resources before reaching for locks.65- Put process-specific behavior in test settings only, not production settings.66- Split large test groups only after profiling shows an imbalance.6768## Common Mistakes6970- Assuming database transaction isolation protects caches, files, task queues, or external systems.71- Mutating class attributes, globals, app settings, or cache entries without restoring them.72- Enabling random order without preserving the seed.73- Using a single lockfile for too much work and accidentally serializing the suite.74- Letting pytest-xdist split related tests so finely that setup cost is duplicated.75- Making parallelism the default before the suite passes reliably under order-isolation checks.7677## Verification7879Before calling parallelization complete:8081- Serial run passes.82- Reverse or random-order run passes, or known failures are fixed.83- Parallel run passes at least twice.84- Any shared-resource sharding lives in test configuration.85- CI has a serial fallback command for debugging.