django-targeted-mocking
Installation
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: