title: how to mock dependency in python
body: <p>I am new to python unit testing framework and lot of confusion in mocking dependency.</p>
  
  <p>I am trying to write unit tests for below member function of a class, (<code>check_something()</code>):</p>
  
  <pre><code>class Validations:    
      def check_something(self):
          abc = os.environ['PLATFORM']
          xyz = Node()
          no_of_nodes = len(xyz.some_type_var)
          if abc != "X_PLATFORM" or no_of_nodes != 1:
              raise someException()
  </code></pre>
  
  <p>How do we eliminate dependency ?</p>
  
  <ol>
  <li>Need to mock <code>Node()</code> ?</li>
  <li>How do we make sure <code>abc</code> is assigned with <code>X_PLATFORM</code> ?</li>
  <li><p>How to assign value <code>1</code> to variable <code>no_of_nodes</code>? which is in turn derived from <code>Node()</code> object.</p>
  
  <pre><code>class Node(object):
      def __init__(self):
          self.nodes = DEF()
          self.some_type_var = someclass().getType()
          self.localnode = os.environ['HOSTNAME']
          self.peertype = self.get_peer_type()
  
      def get_peer_type(self):
          return node
  </code></pre></li>
  </ol>
  
  <p>I tried writing below unit test. I am unable to check for fail and pass condition. I am not sure whether it is correct or not. </p>
  
  <pre><code>class TestValidation(unittest.TestCase):
  
      @mock.patch.object(Node, "get_peer_type")
      @mock.patch('somefile.Node', spec=True)
      def test_1(self, mock_object1, mock_object2):
          os.environ['PLATFORM'] = 'X_PLATFORM'
          obj = Validations()
          self.assertRaises(someException, obj.check_something)
  </code></pre>
  
  <p>Validation class uses <code>Node()</code> Class object and Node class uses some other class.</p>
  
  <ol>
  <li>How to make sure exception is raised or not depending on the condition?</li>
  </ol>