Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Explains when to use Python mocks, fakes, and patching for test isolation while keeping behavioral assertions primary.
Use this skill ↓86% 91%+5 ptsof test cases passed
Passed 20 of 22 test cases with the skill, and 19 of 22 without it.
We are auditing our python unit tests for `report_generator.py` which reads template files from disk. Give guidance on isolation choices and record the mandatory verification summary before concluding.
records the fake, patch, or real dependency choice made
3 of 3 safety checks passed
| Situation | Prefer |
|---|---|
| External API, email, payment, network | Fake service or mock server |
| Clock/time | Explicit time parameter or time-freezing helper |
| Small protocol with meaningful state | Hand-written fake |
| Imported collaborator in one module | unittest.mock.patch at lookup path |
| Complex interface that may drift | autospec, spec, or spec_set |
| Framework setting or request object | Framework test helper |
Use fakes when behavior matters:
class FakeMailer:
def __init__(self):
self.messages = []
def send(self, to, subject, body):
self.messages.append((to, subject, body))
Fakes are especially useful when assertions should read like domain outcomes.
Patch the symbol used by the module under test:
with patch("accounts.views.send_login_email", autospec=True) as send:
response = client.post("/login/", {"email": "[email protected]"})
Patching the original library path may do nothing if the module already imported the dependency.
Skills that do similar work, with their benchmark result.
Next to this one in the catalogue, plus more from the same author.