SOARDO
Software Engineer (SDE)pythonintermediate3 interactive MCQs in app

unittest.mock & pytest-mock

patch where imported/used, not where definedside_effect for simulating exceptions & sequencesautospec=True catching signature mismatches

Mocking isolates unit tests from slow/flaky external boundaries (HTTP APIs, clocks, emails) cleanly using `unittest.mock.patch` or pytest `monkeypatch`. Golden rule: **patch where an object is LOOKED UP / IMPORTED, not necessarily where it was defined!** (If `service.py` ran `from client import fetch_grade`, patch `"service.fetch_grade"`, NOT `"client.fetch_grade"`!) Always check BOTH return behavior AND `assert_called_once_with(...)` / real error behavior (`side_effect=TimeoutError`).

when `service.py` imports `from api import fetch`, calling `@patch("service.fetch", autospec=True, side_effect=[TimeoutError(), {"ok": True}])` exercises retry logic cleanly without network traffic!
patching the original module after `from ... import ...` already bound a local reference in the caller module, leaving tests hitting live network!
Python Mock Object Library — Real Pythonread ↗unittest.mock & pytest-mock — Applied Practicepractice ↗Exercism Python Track (Interactive)practice ↗

Practice unittest.mock & pytest-mock with 3 graded MCQs & written feedback.

Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides