title: How to unit test instance variables initialized outside __init__ method
body: <p>I have a class that initializes an instance variable in a method other than <code>__init__</code>. That instance variable is further referred to in the <code>__init__</code> method.</p>
  
  <p>In the code below, <code>ProductionClass</code> is the class I am testing. In <code>ProductionClass</code>, <code>__init__</code> makes a call to method <code>a_method</code>. Method <code>a_method</code> initializes an instance variable <code>inst_var_2</code>. <code>inst_var_2</code> is used by <code>__init__</code> to set another instance variable <code>inst_var_3</code>.</p>
  
  <pre><code>class LibraryClass(object):
      def __init__(self):
          self.some_attrib = "some_attrib_value"
  
  class ProductionClass(object):
      def __init__(self, arg_one):
          self.inst_var_1 = arg_one
          self.a_method()
          self.inst_var_3 = self.inst_var_2.some_attrib
  
      def a_method(self):
          self.inst_var_2 = LibraryClass()
  
  
  import unittest
  from unittest.mock import patch, MagicMock
  
  class ProdTestCase(unittest.TestCase):
  
      @patch.object(ProductionClass, 'a_method')
      def test_init_method(self, mock_method):
  
          instance = ProductionClass(1234)
          self.assertEqual(1234, instance.inst_var_1)
          mock_method.assert_called_once_with()
  </code></pre>
  
  <p>I am trying to unit test the <code>__init__</code> method of <code>ProductionClass</code> and I am unable to create an instance of the <code>ProductionClass</code>, due to missing instance variable <code>inst_var_2</code>. Here is the error message:</p>
  
  <pre><code>Traceback (most recent call last):
  
    ...
  
    File "/...     production_class.py", line 9, in __init__
      self.inst_var_3 = self.inst_var_2.some_attrib
  AttributeError: 'ProductionClass' object has no attribute 'inst_var_2'
  </code></pre>
  
  <p>My question, is it possible to unit test the <code>__init__</code> method while I am mocking the <code>a_method</code>. I want to mock <code>a_method</code> because I don't want the method to instantiate <code>LibraryClass</code>.</p>