title: Python unittest call function when assertion fails
body: <p><strong>Aim:</strong> I need to call a bunch of steps (hence a function) only if a test fails. </p>
  
  <p><strong>What I tried:</strong></p>
  
  <p>1) Tried passing function w/o arguments. </p>
  
  <p><strong>Observation :</strong> If test passes, function doesn't get called. But if test fails, I get an error. <code>(AssertionError: &lt;bound method TestAuto.func1 of &lt;test_fail.TestAuto testMethod=test_fail&gt;&gt;)</code></p>
  
  <pre><code>class TestAuto(unittest.TestCase):
      def test_fail(self):
          self.assertEqual(1, 1, self.func1)
      def func1(self):
          print 'We are inside'
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p><strong>:</strong></p>
  
  <pre><code>test_fail (test_fail.TestAuto) ... ok
  
  ----------------------------------------
  Ran 1 test in 0.001s
  
  OK
  </code></pre>
  
  <p>2) Tried calling function with arguments.</p>
  
  <pre><code>class TestAuto(unittest.TestCase):
      def test_fail(self):
          self.assertEqual(1, 1, self.func1('message'))
      def func1(self, msg):
          print msg
  </code></pre>
  
  <p><strong>Observation :</strong> Function gets called no matter test passes or fails.</p>
  
  <p><strong>Result:</strong></p>
  
  <p>test_fail (test_fail.TestAuto) ... message
  ok</p>
  
  <hr>
  
  <p>Ran 1 test in 0.001s</p>
  
  <p>OK</p>