Reusing Mock to create attribute mocks unittest.patch
<pre><code>from unittest.mock import patch


class A:
    def b(self):
        return 'HAHAHA A'
a = A()

with patch('__main__.A') as a_mock:
    a_mock.b.return_value = 'not working'
    print(a.b())

HAHAHA A
&gt;&gt;&gt;
</code></pre>

<p>Why doesn't it print <code>'not working'</code>? What for is a_mock, then? ______________</p>

<p>You may want to patch your instance instead of your class:</p>

<pre><code>with patch('__main__.a') as a_mock:
    a_mock.b.return_value = 'works perfectly ;)'
    print(a.b())
</code></pre>

python mocking python-unittest