跳到主要内容
知仓学习社ZHICANG

django-targeted-mocking

Mock Django test boundaries safely with override_settings, pytest settings, autospecced unittest.mock objects, output/input capture, requests-mock, …

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少2 个文本文件,5 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

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

  1. Decide whether mocking is necessary.
  • Prefer real code for local domain logic.
  • Mock external HTTP, time, settings, output/input, and expensive service edges.
  1. 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.
  1. Prefer specific tools.
  • Settings: override_settings, modify_settings, or pytest-django's settings fixture.
  • Output: io.StringIO, pytest capsys, or command test helpers.
  • HTTP: requests-mock for hand-authored responses; VCR.py for recorded responses.
  • Time: pass time as a parameter when possible; otherwise use time-machine or a similar time-specific tool.
  1. Make mocks interface-aware.
  • Use autospec=True, spec, spec_set, create_autospec, or a small fake object.
  • Avoid bare Mock/MagicMock when the interface matters.
  1. Block unexpected outbound HTTP.
  • A normal test run should fail if a test reaches the network unintentionally.

Read [mocking-patterns.md](references/mocking-patterns.md) for concrete patterns.

Decision Rules

  • Use Django settings helpers instead of direct assignment to django.conf.settings.
  • Prefer a hand-built fake or types.SimpleNamespace when only a few attributes are needed.
  • Use requests-mock for deterministic API responses you control.
  • Use VCR.py when real recorded responses are valuable and safe to store.
  • On CI, configure VCR record mode so missing cassettes fail instead of recording.
  • Mock time consistently across the runtime; do not patch several datetime functions by hand.
  • Do not mock the component whose behavior the test is meant to prove.

Common Mistakes

  • Patching settings directly and leaking state across tests.
  • Using catch-all MagicMock objects that accept typoed attributes and wrong call signatures.
  • Allowing tests to hit real external services.
  • Recording VCR cassettes in CI.
  • Mocking a view's internal helpers so thoroughly that the test no longer covers request behavior.
  • Patching the wrong import path and thinking the mock is active.

Verification

Before finishing:

  • Each mock has a clear boundary reason.
  • Interface-sensitive mocks use specs or fakes.
  • Unexpected outbound HTTP is blocked in the normal suite.
  • Settings are restored automatically by Django or pytest helpers.
  • Tests still cover the behavior, not only mock calls.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。