1

I am trying to execute post-failure operations; so I can gather info on the state of the system only if it fail.

So far, I did not find a solution that works. I did try to add a try-except and this works; but then the output of my test run is "success", which is totally wrong.

try:
    self.assertFalse(x>1, "the test failed")
except Exception as e:
    print(e)
    # do other post failure actions

Since the exception is caught, I assume that the unit test class won't be involved in reporting the error, and this end up with the test not failing.

How do I "escalate" the failure at the same time to both the except section and the Unit test class?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

1 Answers1

4

You can re-raise the exception once you caught and recorded it:

try:
    self.assertFalse(x>1, "the test failed")
except Exception as e:
    print(e)
    # do other post failure actions

    raise

Note that you may and should be more specific about the error you are catching - in this case, you are looking for catching the AssertionError exception instead of the generic Exception.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195