django-test-data
Design faster, clearer Django test data and test structure with factories, setUpTestData, SimpleTestCase/TestCase choices, fixture-file cleanup, que…
它会碰到什么
扫了多少2 个文本文件,6 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Django Test Data
Most slow Django suites spend time building data they do not need or exercising full request/database paths for behavior that can be tested at a smaller boundary. Use this skill to reduce setup cost while keeping representative integration coverage.
Refactoring Workflow
- Map the behavior under test.
- Identify the smallest useful boundary: function, form, model method, middleware, command helper, view, or full request path.
- Keep a few integration tests for wiring; move detailed cases to unit tests where the boundary is clean.
- Choose the fastest test base class.
SimpleTestCase: no database access.TestCase: ordinary database tests with rollback.TransactionTestCase: committed transaction behavior only.LiveServerTestCase: browser/live-server tests only.
- Remove broad fixture data.
- Avoid large fixture files and base classes that always create objects.
- Build only the data each test or class needs.
- Use factories deliberately.
- Start with small factory functions when the domain is simple.
- Use Factory Boy or Model Bakery when relationships and variants become repetitive.
- Share class-level data with
setUpTestData.
- Use
setUpTestData()for database objects reused by multiple methods in aTestCase. - Avoid mutating shared in-memory objects across methods.
- Optimize database access in test setup and assertions.
- Use
select_related,prefetch_related, orbulk_createwhere setup/query cost is the bottleneck. - Assert query counts for hot paths when performance is part of the contract.
Read [patterns.md](references/patterns.md) for examples and decision details.
Decision Rules
- If a test does not need the database, use
SimpleTestCase. - If only some tests need the database, split them into separate classes.
- If a test requires committed transaction behavior, first check whether
captureOnCommitCallbacks()or an inneratomic()is enough. - If many tests share expensive objects, use
setUpTestDatainstead ofsetUp. - If fixture files are hard to understand or grow over time, replace them with factories.
- If a test depends on hard-coded auto-increment IDs, fix the assertion rather than enabling
reset_sequences=True. - Combine assertions when they describe one behavior produced by one expensive action.
Common Mistakes
- Testing form validation only through rendered HTML instead of inspecting form errors directly.
- Leaving management-command business logic inside
handle(), forcing tests throughcall_command(). - Using
TransactionTestCaseas the default. - Putting data in a base
TestCasethat only a few subclasses need. - Treating factories as permission to create a large object graph for every test.
- Mutating objects created by
setUpTestDataand leaking in-memory state to later tests.
Verification
Before finishing a refactor:
- The old behavior remains covered at the right level.
- Database-using and non-database tests are split where useful.
- Repeated setup moved to
setUpTestDataor factories only where it reduces cost. - Relevant tests pass individually and as part of their module/class.
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 1,027
本站分层T1
该仓技能数1910
原文件路径
plugins/LVTD-LLC/skills/skills/django-test-data/SKILL.md