django-test-parallelization
Safely parallelize Django and pytest-django test suites with Django --parallel, pytest-xdist, test isolation checks, randomized order detection, sha…
它会碰到什么
扫了多少2 个文本文件,6 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
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 runpython manage.py test --parallel. - pytest-django: install
pytest-xdist, then runpytest -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](references/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 loadscopefor DjangoTestCase-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.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 1,027
本站分层T1
该仓技能数1910
原文件路径
plugins/LVTD-LLC/skills/skills/django-test-parallelization/SKILL.md