title: How to mock an instance method in Python
body: <p>I'm trying to test a class and mock one of its methods, but I can't seem to replace the behavior of one of the instance methods with my mocked behavior. My code is organized like so:</p>
  
  <p><code>
  --src
  ----module
  ------__init__.py
  ------my_class.py
  --tst
  ----__init__.py
  ----test_my_class.py
  </code></p>
  
  <p><code>my_class.py</code> contains the following</p>
  
  <pre><code>class MyClass:
  
      def __init__(self):
          pass
  
      def do_something(self):
          return 'Real Output'
  </code></pre>
  
  <p>My test file <code>test_my_class.py</code> contains the following.</p>
  
  <pre><code>from unittest.mock import patch
  from src.module.my_class import MyClass
  
  def test_my_class():
      my_class = MyClass()
      assert my_class.do_something() == 'Real Output'
  
  @patch('src.module.my_class.MyClass')
  def test_mock_my_class(mock_my_class):
      mock_my_class.return_value.do_something.return_value = 'Mocked Output'
      my_class = MyClass()
      assert my_class.do_something() == 'Mocked Output'
  </code></pre>
  
  <p>The first test works just fine (no mocking involved so far). The second test, however, gives me the following assertion error. I expect the <code>do_something()</code> method to be mocked and to return "Mocked Output", and for the assert statement to evaluate to true. Where am I going wrong here?</p>
  
  <p><code>AssertionError: assert &lt;bound method MyClass.do_something of &lt;src.module.my_class.MyClass object at 0x1057133c8&gt;&gt; == 'Mocked Output'
  E        +  where &lt;bound method MyClass.do_something of &lt;src.module.my_class.MyClass object at 0x1057133c8&gt;&gt; = &lt;src.module.my_class.MyClass object at 0x1057133c8&gt;.do_something</code></p>
  
  <p>PS. I've consulted the following resources without success:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/17731477/python-mock-class-instance-variable">Python mock class instance variable</a></li>
  <li><a href="https://stackoverflow.com/questions/44442510/mocking-methods-on-an-instance-variable-in-python">Mocking Methods on an Instance Variable in Python</a></li>
  <li><a href="https://stackoverflow.com/questions/44768483/python-mock-wrap-instance-method">Python mock: wrap instance method</a></li>
  <li><a href="https://stackoverflow.com/questions/38579535/how-to-supply-a-mock-class-method-for-python-unit-test">How to supply a mock class method for python unit test?</a></li>
  <li><a href="https://medium.com/python-pandemonium/python-mocking-you-are-a-tricksy-beast-6c4a1f8d19b2" rel="nofollow noreferrer">https://medium.com/python-pandemonium/python-mocking-you-are-a-tricksy-beast-6c4a1f8d19b2</a></li>
  </ul>
  
  <p>That last link looked especially helpful at first, because I'm following one of the examples almost verbatim, but it still doesn't work.</p>
  
  <pre><code>@mock.patch("simple.SimpleClass")
  def mock_simple_class(mock_class):
      mock_class.return_value.explode.return_value = "BOO!"
      inst = simple.SimpleClass()
      result = inst.explode()
      print(result)
  </code></pre>