django-test-data
Design faster, clearer Django test data and test structure with factories, setUpTestData, SimpleTestCase/TestCase choices, fixture-file cleanup, query optimization, and unit-vs-integration boundaries. Use when Django tests create too much data, rely on slow fixtures, overuse TransactionTestCase, duplicate setup, or need refactoring for speed without losing coverage.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- django-test-data
- license
- MIT
- metadata
- { "tags": "django,testing,test-data,factories,pytest", "version": "0.1.0", "category": "Django", "displayName": "Django Test Data" }
- description
- Design faster, clearer Django test data and test structure with factories, setUpTestData, SimpleTestCase/TestCase choices, fixture-file cleanup, query optimization, and unit-vs-integration boundaries. Use when Django tests create too much data, rely on slow fixtures, overuse TransactionTestCase, duplicate setup, or need refactoring for speed without losing coverage.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
- 参考文档references/patterns.md2.5 KB
03 SKILL.md 正文
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.
- Use
-
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.
- Use
Read 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.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.