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-13
- Stars
- ★ 1.6k
- Forks
- 483
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
migrate-to-e2e
Moves an existing Playwright, Cypress, Selenium, Appium, Detox, Maestro, or TesterArmy suite to e2e one slice at a time, with the old suite running beside the new one until each ported test is proven. Keeps a MIGRATION.md progress file so any later session resumes where the last stopped. Optionally plants a defect to show both suites catch it. Use for /migrate-to-e2e, "migrate our tests to e2e", "port these Maestro flows", "move our TesterArmy tests", or when a project has an e2e config and an older suite still running.
e2e
Writes, runs, and debugs end-to-end tests for web and mobile apps with e2e, the agentic test runner, which mixes agent.act goals with exact locator assertions on a Playwright browser or an iOS or Android device. Also drives a project's running app live over the e2e MCP server to look at a screen, check a UI change, take a screenshot or record a video, and try a locator; and runs bug bashes, parallel e2e explore runs with each bug proven by a repro test. Use when a project depends on e2e or has an e2e.config.ts, when asked for end-to-end, browser, mobile, or agentic UI tests, to check a change in the running app, to QA or bug bash an app or branch, or when an e2e run fails.
create-verification-skill
Generate a project-local verify-<app> skill on top of e2e, so any coding agent can launch the app, check it is worth driving, exercise a feature the way a user does, keep evidence, and bug bash it. Writes the skill, a feature map with one explore charter per feature, and proves it once. Use for /create-verification-skill, "make a verification skill for this repo", or when a project has no scripted way to prove UI behavior.
deja-search
deja-vu memory — search the user's past AI coding sessions with the deja CLI. Holds what they already settled and never wrote into the repo (naming rules, banned or already-tried tools, required checks before merge, preferred values, who owns what). Use before your first edit in a task, before you call a change done or ready to merge, before you recommend a tool, library, design or reviewer, before re-debugging an error that may already be solved, when they say 'didn't we fix this before', 'what did we decide about X' or 'I already have that', and before telling them something on this machine does not exist.
deja-reflect
End-of-session reflection and memory curation with deja-vu. Review what was decided, what commands broke and what repaired them, and record durable project rules or decisions with deja remember so the next session starts with them.
revid
Make, edit, export and post AI videos with Revid (revid.ai) through the Revid MCP server. Use whenever the user wants a short-form or long-form video made (from an idea, a script, a URL, an article, a PDF, a tweet, a product page, their own clips, or a song), wants to find what is going viral on TikTok, YouTube, Instagram or X before making something, wants to edit or re-export an existing Revid project, generate a standalone AI image or clip, make a series episode, or publish and schedule a finished video to connected social accounts. Covers setup (hosted MCP with OAuth, or an API key), the 36 tools, credits, polling and the rules for publishing. Routes to the more specific revid-* skills when one fits.