title: Mocking Method Calls In Python
body: <p>I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt:</p>
  
  <pre><code>import unittest
  from unittest.mock import MagicMock
  import openpyxl 
  from MyPythonFile import MyClass
  
  class TestMyClass(unittest.TestCase):
    def test_myclass(self):
      myclass = MyClass()
      wb = openpyxl.workbook()
      ws = openpyxl.worksheet()
      wbPath = 'wbPath'
  
      openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)
  </code></pre>
  
  <p>When I try the final line I get the error "can't assign to function call". Do I need to use <code>patch.object('openpyxl','load_workbook')</code>? I am used to mocking in Java with Groovy and it's pretty straightforward.</p>
  
  <p>*Edit: wanted to add the finalized version of the test based on the response from @alxwrd </p>
  
  <pre><code>import unittest
  from unittest.mock import MagicMock
  import openpyxl 
  import configparser
  from MyPythonFile import MyClass
  
  class TestMyClass(unittest.TestCase):
    def test_myclass(self):
      myclass = MyClass()
      wb = openpyxl.workbook()
      ws = openpyxl.worksheet()
      config = configparser.ConfigParser()
  
      openpyxl.load_workbook = MagicMock(return_value=wb)
      wb.get_sheet_by_name = MagicMock(return_value=ws)
  
      config.sections() = MagicMock(return_value=['Section1'])
      config.get = MagicMock(side_effect=['Value1','Value2']) 
  </code></pre>
  
  <p>Notice that config.get gives multiple returns with the side_effect parameter, so if <code>config.get()</code> is called once in the code it returns <code>'Value1'</code> and when <code>config.get()</code> is called a second time it returns <code>'Value2'</code>.</p>