title: Mock parent class __init__ method
body: <p>I am trying to write some unittests on a class which is derived from another, but I have some difficulties to mock the parent class <strong>init</strong> method, which afaik you cannot, so I am looking for suggestion.</p>
  
  <p>Here an example of how are my classes</p>
  
  <p>Imported.py</p>
  
  <pre><code>class Imported():
      def __init__(self, a="I am Imported"):
          print("a:{}".format(a))
  </code></pre>
  
  <p>Parent.py</p>
  
  <pre><code>from Imported import Imported
  
  class Parent(object):
  
      parent_list = ["PARENT"]
  
      def __init__(self, a="I am Parent"):
          imported = Imported(a)
  </code></pre>
  
  <p>Derived.py        </p>
  
  <pre><code>from Parent import Parent
  
  class Derived(Parent):
  
      Parent.parent_list.append("DERIVED")
  </code></pre>
  
  <p>In my unittests I want to verify that Parent.parent_list == ["PARENT", "DERIVED"] when I instantiate an object from the Derived class, Derived().</p>
  
  <p>Both of this solution doesn't work</p>
  
  <p>test_Derived.py</p>
  
  <pre><code>import unittest
  from mock import patch
  
  from Derived import Derived
  
  
  class test_Derived(unittest.TestCase):
  
      @patch("Derived.Parent.__init__")
      def test_init_001(self, mock_parent_init):
          a = Derived("I am Derived")
          mock_parent_init.assert_called_with("I am Derived")
          self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])
  
      @patch("Derived.Imported.Imported")
      def test_init_002(self, mock_parent_init):
          a = Derived("I am Derived")
          mock_parent_init.assert_called_with("I am Derived")
          self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])
  </code></pre>
  
  <p>test_init_001 fails with</p>
  
  <pre><code>TypeError: __init__() should return None, not 'MagicMock'
  </code></pre>
  
  <p>test_init_002 fails with</p>
  
  <pre><code>ImportError: No module named Parent.Imported
  </code></pre>
  
  <p>Any suggestion?   </p>