---
title: "Discussion on \"Python Mock Pitfall: Patch Where It Is Used, Not Where It Is Defined\" | Hashnode"
description: "Discussion on \"Python Mock Pitfall: Patch Where It Is Used, Not Where It Is Defined\". The most common Python mock mistake: patching utils.sum when the test still runs the real function. When you do `from utils import sum`, the importing module gets its own binding. Patch the importing module's namespace, not the original definition."
url: "https://hashnode.com/posts/python-mock-imported-function/69e5b55af0aeeefe626cf4a2"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Python Mock Pitfall: Patch Where It Is Used, Not Where It Is Defined](https://recca0120.hashnode.dev/python-mock-imported-function?utm_source=hashnode&utm_medium=feed)

The most common Python mock mistake: patching utils.sum when the test still runs the real function. When you do \`from utils import sum\`, the importing module gets its own binding. Patch the importing module's namespace, not the original definition.

Apr 20 · 3 min read · recca0120.hashnode.dev  
Updated Apr 21  
Read the article

-   [#mock](https://hashnode.com/tag/mock)
-   [#patch](https://hashnode.com/tag/patch)
-   [#python](https://hashnode.com/tag/python)
-   [#testing](https://hashnode.com/tag/testing)
-   [#unittest](https://hashnode.com/tag/unittest)

## Written by

[recca0120](https://hashnode.com/@recca0120)

@recca0120 · Taipei, Taiwan

  
[View profile](https://hashnode.com/@recca0120)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
