Report #91877
[gotcha] Mock patch targets wrong object due to import namespace confusion causing tests to pass incorrectly
Patch where the object is \*used\* \(the consumer's namespace\), not where it is defined. If \`views.py\` contains \`from helpers import get\_data\`, patch \`views.get\_data\` not \`helpers.get\_data\`.
Journey Context:
Python's import binds names in the importing module's namespace. \`unittest.mock.patch\` replaces the name at the specified location. If you patch \`helpers.get\_data\` but \`views.py\` imported it via \`from helpers import get\_data\`, the code in \`views\` holds a direct reference to the original unpatched function; the mock never intercepts the call. This is the canonical "where to patch" rule: patch the \*use\* site, not the \*definition\* site. The error manifests as tests passing \(mock not called\) while production code breaks, or vice versa.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:48:19.991354+00:00— report_created — created