django-targeted-mocking
Mock Django test boundaries safely with override_settings, pytest settings, autospecced unittest.mock objects, output/input capture, requests-mock, VCR.py, outbound HTTP blocking, and time-freezing tools. Use when tests need to replace settings, HTTP calls, time, command output, stdin, or external services without broad fragile MagicMock patches.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- django-targeted-mocking
- license
- MIT
- metadata
- { "tags": "django,testing,mocking,http,pytest", "version": "0.1.0", "category": "Django", "displayName": "Django Targeted Mocking" }
- description
- Mock Django test boundaries safely with override_settings, pytest settings, autospecced unittest.mock objects, output/input capture, requests-mock, VCR.py, outbound HTTP blocking, and time-freezing tools. Use when tests need to replace settings, HTTP calls, time, command output, stdin, or external services without broad fragile MagicMock patches.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
- 参考文档references/mocking-patterns.md1.9 KB
03 SKILL.md 正文
Django Targeted Mocking
Mock only at boundaries that are slow, nondeterministic, external, or hard to trigger. Broad mocks can make tests faster while proving less, so prefer framework helpers and interface-checked replacements.
Boundary Workflow
-
Decide whether mocking is necessary.
- Prefer real code for local domain logic.
- Mock external HTTP, time, settings, output/input, and expensive service edges.
-
Patch where the code under test looks up the dependency.
- Patch the imported symbol used by the module under test, not the original library path unless that is what the module reads.
-
Prefer specific tools.
- Settings:
override_settings,modify_settings, or pytest-django'ssettingsfixture. - Output:
io.StringIO, pytestcapsys, or command test helpers. - HTTP:
requests-mockfor hand-authored responses; VCR.py for recorded responses. - Time: pass time as a parameter when possible; otherwise use
time-machineor a similar time-specific tool.
- Settings:
-
Make mocks interface-aware.
- Use
autospec=True,spec,spec_set,create_autospec, or a small fake object. - Avoid bare
Mock/MagicMockwhen the interface matters.
- Use
-
Block unexpected outbound HTTP.
- A normal test run should fail if a test reaches the network unintentionally.
Read mocking-patterns.md for concrete patterns.
Decision Rules
- Use Django settings helpers instead of direct assignment to
django.conf.settings. - Prefer a hand-built fake or
types.SimpleNamespacewhen only a few attributes are needed. - Use
requests-mockfor deterministic API responses you control. - Use VCR.py when real recorded responses are valuable and safe to store.
- On CI, configure VCR record mode so missing cassettes fail instead of recording.
- Mock time consistently across the runtime; do not patch several datetime functions by hand.
- Do not mock the component whose behavior the test is meant to prove.
Common Mistakes
- Patching settings directly and leaking state across tests.
- Using catch-all
MagicMockobjects that accept typoed attributes and wrong call signatures. - Allowing tests to hit real external services.
- Recording VCR cassettes in CI.
- Mocking a view's internal helpers so thoroughly that the test no longer covers request behavior.
- Patching the wrong import path and thinking the mock is active.
Verification
Before finishing:
- Each mock has a clear boundary reason.
- Interface-sensitive mocks use specs or fakes.
- Unexpected outbound HTTP is blocked in the normal suite.
- Settings are restored automatically by Django or pytest helpers.
- Tests still cover the behavior, not only mock calls.
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.