title: Avoid executing __init__ of mocked class
body: <p>I have a class with an expensive <code>__init__</code> function. I don't want this function called from tests.</p>
  
  <p>For the purpose of this example, I made a class that raises an Exception in <code>__init__</code>:</p>
  
  <pre><code>class ClassWithComplexInit(object):
  
      def __init__(self):
          raise Exception("COMPLEX!")
  
      def get_value(self):
          return 'My value'
  </code></pre>
  
  <p>I have a second class that constructs an instance of <code>ClassWithComplexInit</code> and uses it's function.</p>
  
  <pre><code>class SystemUnderTest(object):
  
      def my_func(self):
          foo = ClassWithComplexInit()
          return foo.get_value()
  </code></pre>
  
  <p>I am trying to write some unit tests around <code>SystemUnderTest#my_func()</code>. The problem I am having is no matter how I try to mock <code>ClassWithComplexInit</code>, the <code>__init__</code> function always gets executed and the exception is raised.</p>
  
  <pre><code>class TestCaseWithoutSetUp(unittest.TestCase):
  
      @mock.patch('mypackage.ClassWithComplexInit.get_value', return_value='test value')
      def test_with_patched_function(self, mockFunction):
          sut = SystemUnderTest()
          result = sut.my_func()  # fails, executes ClassWithComplexInit.__init__()
          self.assertEqual('test value', result)
  
      @mock.patch('mypackage.ClassWithComplexInit')
      def test_with_patched_class(self, mockClass):
          mockClass.get_value.return_value = 'test value'
          sut = SystemUnderTest()
          result = sut.my_func()  # seems to not execute ClassWithComplexInit.__init__()
          self.assertEqual('test value', result)  # still fails
          # AssertionError: 'test value' != &lt;MagicMock name='ClassWithComplexInit().get_value()' id='4436402576'&gt;
  </code></pre>
  
  <p>The second approach above is one that I got from <a href="https://stackoverflow.com/a/26547477/346561">this similar Q&amp;A</a> but it didn't work either. It <em>seemed</em> to not run the <code>__init__</code> function but my assertion fails because the result ends up being a mock instance as opposed to my value.</p>
  
  <p>I also tried to configure a <code>patch</code> instance in the <code>setUp</code> function, using the <code>start</code> and <code>stop</code> functions as <a href="http://www.voidspace.org.uk/python/mock/patch.html#patch-methods-start-and-stop" rel="noreferrer">the docs suggest</a>.</p>
  
  <pre><code>class TestCaseWithSetUp(unittest.TestCase):
  
      def setUp(self):
          self.mockClass = mock.MagicMock()
          self.mockClass.get_value.return_value = 'test value'
          patcher = mock.patch('mypackage.ClassWithComplexInit', self.mockClass)
          patcher.start()
          self.addCleanup(patcher.stop)
  
      def test_my_func(self):
          sut = SystemUnderTest()
          result = sut.my_func()  # seems to not execute ClassWithComplexInit.__init__()
          self.assertEqual('test value', result)  # still fails
          # AssertionError: 'test value' != &lt;MagicMock name='mock().get_value()' id='4554658128'&gt;
  </code></pre>
  
  <p>This also seems to avoid my <code>__init__</code> function but the value I set for <code>get_value.return_value</code> isn't being respected and <code>get_value()</code> is still returning a <code>MagicMock</code> instance.</p>
  
  <p>How can I mock a class with an complicated <code>__init__</code> which is instantiated by my code under test? Ideally, I would like a solution which works well for many unit tests within a TestCase class (e.g. Not needing to <code>patch</code> every test).</p>
  
  <p>I am using Python version <code>2.7.6</code>.</p>