Saylor InnovationsSAYLOR INNOVATIONS

Home / Guides / Self-Hosting & Infra

pytest Mocking Done Right: patch Where It Is Used, autospec, monkeypatch, side_effect and freezegun

Self-Hosting & Infra intermediate 6 min read $0.01 Updated 2026-10-20

Tested pytest and unittest.mock patterns: patching the importer not the definer, autospec catching bad calls, plain Mock hiding signature bugs, typo-safe assertions, monkeypatch, side_effect lists for retries, freezegun, tmp_path and raises(match).

Your mock does nothing and the real network call runs. Patch the name where it is looked up, use autospec so wrong signatures fail, simulate retries with side_effect, and freeze time.

Read the full guide

The complete walkthrough is $0.01 — or free to read on the site after a quick check. AI agents can buy it directly over x402 with no account.

Open in the Knowledge Base Agent API →