4

I'm using unittest.

In case on of my tests in the testcase fails, I like to do something (e.g., save the erroneous output to a temporary folder for later review, etc.).

Where does this code belong?

At first, I thought I could check if self.assertEqual(...), but it turns out this function doesn't return any value. It makes sense now, since it is intended to kick the execution out of the test function once failure is detected.

tearDown is called regardless of the test success, so it doesn't seem to help either.

max
  • 49,282
  • 56
  • 208
  • 355
  • Does this help? http://stackoverflow.com/a/284326/1240268 – Andy Hayden Sep 11 '12 at 10:06
  • @hayden It does; but I want to do something more complicated than just produce output (most of our test failures are due to the intentional changes in the code, so I want to actually ask user if they want to validate the new output based on a brief examination of the differences). – max Sep 11 '12 at 10:12
  • @oers: I think the link is about some other language (Java? I'm not sure). My question is about Python, so I guess the answers wouldn't be the same. – max Sep 11 '12 at 10:14
  • @max sorry :) I totally read this as java – oers Sep 11 '12 at 10:14

1 Answers1

4

One way would be to set a flag on the test case instance and then check its value upon tear down:

def setUp(self):
    self.test_passed = false

def tearDown(self):
    if not self.test_passed:
        log()

def test_something(self):
    self.assertEquals(something())
    self.test_passed = true

You could write a decorator to avoid the need to set your flag to true at the end of every test.

zifot
  • 2,688
  • 20
  • 21
  • That would work. Assuming I don't have multiple `return` statements in the test methods (which I shouldn't) it would require just a single `self.test_passed = true` line at the end of each test method. – max Sep 11 '12 at 10:15