Patch method only in one module
<p>For example, I have some module(<code>foo.py</code>) with next code:</p>

<pre><code>import requests

def get_ip():
    return requests.get('http://jsonip.com/').content
</code></pre>

<p>And module <code>bar.py</code> with similiar code:</p>

<pre><code>import requests

def get_fb():
    return requests.get('https://fb.com/').content
</code></pre>

<p>I just can't understand why next happens:</p>

<pre><code>from mock import patch

from foo import get_ip
from bar import get_fb

with patch('foo.requests.get'):
    print(get_ip())
    print(get_fb())
</code></pre>

<p>They are two mocked:
<code>
&lt;MagicMock name='get().content' id='4352254472'&gt;
&lt;MagicMock name='get().content' id='4352254472'&gt;
</code>
It is seemed to patch only <code>foo.get_ip</code> method due to <code>with patch('foo.requests.get')</code>, but it is not.
I know that I can just get <code>bar.get_fb</code> calling out of <code>with</code> scope, but there are cases where I just run in context manager one method that calls many other, and I want to patch <code>requests</code> only in one module.
Is there any way to solve this? Without changing imports in module</p>

<p>The two locations <code>foo.requests.get</code> and <code>bar.requests.get</code> refer to the same object, so mock it in one place and you mock it in the other.  </p>

<p>Imagine how you might implement patch.  You have to find where the symbol is located and replace the symbol with the mock object.  On exit from the with context you will need to restore the original value of the symbol.  Something like (untested):</p>

<pre><code>class patch(object):
    def __init__(self, symbol):
        # separate path to container from name being mocked
        parts = symbol.split('.')
        self.path = '.'.join(parts[:-1]
        self.name = parts[-1]
    def __enter__(self):
        self.container = ... lookup object referred to by self.path ...
        self.save = getattr(self.container, name)
        setattr(self.container, name, MagicMock())
    def __exit__(self):
        setattr(self.container, name, self.save)
</code></pre>

<p>So your problem is that the you are mocking the object in the request module, which you then are referring to from both foo and bar.</p>

<hr>

<p>Following @elethan's suggestion, you could mock the requests module in foo, and even provide side effects on the get method:</p>

<pre><code>from unittest import mock
import requests

from foo import get_ip
from bar import get_fb

def fake_get(*args, **kw):
    print("calling get with", args, kw)
    return mock.DEFAULT

replacement = mock.MagicMock(requests)
replacement.get = mock.Mock(requests.get, side_effect=fake_get, wraps=requests.get)
with mock.patch('foo.requests', new=replacement):
    print(get_ip())
    print(get_fb())
</code></pre>

<hr>

<p>A more direct solution is to vary your code so that <code>foo</code> and <code>bar</code> pull the reference to <code>get</code> directly into their name space.</p>

<p>foo.py:</p>

<pre><code>from requests import get

def get_ip():
    return get('http://jsonip.com/').content
</code></pre>

<p>bar.py:</p>

<pre><code>from requests import get

def get_ip():
    return get('https://fb.com/').content
</code></pre>

<p>main.py:</p>

<pre><code>from mock import patch

from foo import get_ip
from bar import get_fb

with patch('foo.get'):
    print(get_ip())
    print(get_fb())
</code></pre>

<p>producing:</p>

<pre><code>&lt;MagicMock name='get().content' id='4350500992'&gt;
b'&lt;!DOCTYPE html&gt;\n&lt;html lang="en" id="facebook" ...
</code></pre>

<hr>

<p>Updated with a more complete explanation, and with the better solution (2016-10-15)</p>

<p>Note: added <code>wraps=requests.get</code> to call the underlying function after side effect. </p>

python python-3.x python-unittest python-mock python-unittest.mock