Skip to content Skip to sidebar Skip to footer

Python Unittest: Reporting Exception As Failure

I want to check for an exception in Python unittest, with the following requirements: Needs to be reported as a failure, NOT an error Must not swallow the original exception I've

Solution 1:

As suggested, you could use the context of a generic exception:

except Exception, error:
    self.fail("Failed with %s" % error)

You can also retrieve the information relative to the exception through sys.exc_info()

try:
    1./0except:
    (etype, evalue, etrace) = sys.exc_info()
    self.fail("Failed with %s" % evalue)

The tuple (etype, evalue, etrace) is here (<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('float division',), <traceback object at 0x7f6f2c02fa70>)

Post a Comment for "Python Unittest: Reporting Exception As Failure"