18469

Python unittest: Reporting Exception as Failure

Question:

I want to check for an exception in Python unittest, with the following requirements:

<ul><li>Needs to be reported as a failure, NOT an error</li> <li>Must not swallow the original exception</li> </ul>

I've seen lots of solutions of the form:

try: something() except: self.fail("It failed")

Unfortunately these solutions swallow the original exception. Any way to retain the original exception?

I ended up using a variant of Pierre GM's answer:

try: something() except: self.fail("Failed with %s" % traceback.format_exc())

Answer1:

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 <a href="http://docs.python.org/library/sys.html" rel="nofollow">sys.exc_info()</a>

try: 1./0 except: (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>)

Answer2:

You can capture the exception in a variable using something like

except Exception, e: // fail with str(e)

Recommend

  • UPDATE heap table - Deadlock on RID
  • typing.Any vs object?
  • How to close fancybox after Ajax Submit?
  • OpenMP thread mapping to physical cores
  • Update Stripe Credit Card with Coffeescript
  • pip installation of gmpy2
  • expand ipv6 address in shell script
  • Allow QGraphicsView to move outside scene
  • In Java, how can I take a variable obtained from a user input from one method and use the output in
  • Running ASP.NET Web Api 2 application without Visual Studio
  • How to warn user not to leave the page? [duplicate]
  • How to list all Call logs from Log list including Sms and email logs also?
  • ASP.NET grid in 2.0 having trouble with paging to retain value of check box on save button click
  • How to return multiple result in the same query?
  • Download local file in angularJS
  • Get a list of who has what access to git repositories
  • Decompress string in java from compressed string in C#
  • Auto-incrementation with HSQLDB (2.2.8) + DDLUtils
  • Split an Array into 3 arrays [duplicate]
  • How can I return more than one value from a method in C#
  • How to efficiently work with multiple database tables in Ruby on Rails
  • When scaling and drawing an image to canvas in iOS Safari, width is correct but height is squished
  • Retrieve 3rd MAX salary in Hive
  • how to synchronize web site content while loading page
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • analytics.js event not working properly
  • Ways of filling 10 places with number from [1..10] such that digit at ith place has value atmost 1 m
  • Windows: How do I get the mode/access rights of an already opened file?
  • Generate a runnable jar and include libraries in it with Maven
  • Simultaneous animation when entering editing mode of UITableViewCell
  • Tensorflow crash using tf.train.Saver() with GPU
  • Silverlight Event Log in Isolated Storage
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Print Longitude and Latitude in textbox Android
  • How to turn off notice reporting in xampp?
  • Android: Unable to detect vertical plane