python-mock-isolation-design
Use Python mocks and fakes as a design tool without losing behavioral confidence. Use when testing external dependencies, choosing monkeypatch versus unittest.mock.patch, isolating slow boundaries, avoiding mock-heavy tests, interpreting mock call assertions, or refactoring toward clearer dependency seams.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- python-mock-isolation-design
- license
- MIT
- metadata
- { "tags": "python,tdd,testing,mocking,architecture", "version": "0.1.0", "category": "Testing", "displayName": "Python Mock Isolation Design" }
- description
- Use Python mocks and fakes as a design tool without losing behavioral confidence. Use when testing external dependencies, choosing monkeypatch versus unittest.mock.patch, isolating slow boundaries, avoiding mock-heavy tests, interpreting mock call assertions, or refactoring toward clearer dependency seams.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 3
- 参考文档agents/openai.yaml244 B
- 参考文档evals/evals.json828 B
- 参考文档references/mock-isolation-patterns.md2.2 KB
03 SKILL.md 正文
Python Mock Isolation Design
Use this skill when replacing a dependency in a Python test changes what the test proves. Mocks are useful at slow, nondeterministic, or external boundaries; they become harmful when they replace the behavior the test is supposed to validate.
Source Traceability
Primary source: Harry Percival, Test-Driven Development with Python, 3rd ed. Guidance is transformed and paraphrased from chapters 20, 21, 27, and Appendix A, especially manual monkeypatching, unittest.mock.patch, mock coupling, call argument inspection, test isolation, and the architectural route out of mock-heavy suites.
Workflow
-
Name the boundary.
- External service, email, clock, filesystem, network, framework adapter, or expensive side effect.
- If the dependency is local domain logic, prefer real code.
-
Choose the replacement.
- Use a fake when state and behavior are simple and meaningful.
- Use
patchwhen replacing a collaborator looked up by the module under test. - Use monkeypatching sparingly and restore state automatically.
- Use autospec/spec when interface drift matters.
-
Keep behavior assertions primary.
- Assert user-visible or domain-visible outcomes first.
- Assert mock calls only when the interaction is the contract.
- Inspect call arguments when they clarify the behavior, not as a substitute for it.
-
Watch for design feedback.
- If setup requires many mocks, the code may have hidden dependencies.
- If tests break on harmless refactors, the test is too coupled to implementation.
- Consider introducing a boundary interface, service object, or functional core.
Read mock-isolation-patterns.md for replacement choices and smell handling.
Decision Rules
- Patch where the code under test looks up the dependency.
- Prefer fakes for small stable protocols.
- Use
mock.return_valueandside_effectdeliberately; do not let default mocks create imaginary object graphs. - Avoid broad
MagicMockobjects without specs for important interfaces. - Keep at least one integration or contract test for each important boundary.
Guardrails
- Do not mock Django settings, HTTP, time, or command output by hand if a framework helper exists; use
django-targeted-mockingfor Django-specific boundaries. - Do not assert only that a mocked method was called unless that call is the behavior.
- Do not use mocks as the primary strategy for making all tests fast; architecture should carry that work.
- Do not keep mocks that prevent refactoring from changing implementation safely.
Verification
Before finishing, record:
- Boundary being replaced and why.
- Fake, patch, or real dependency choice.
- Behavior assertion that proves the outcome.
- Mock interaction assertion only when interaction is the contract.
- Integration/contract coverage for the real boundary.
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.