nose-devs/nose — Issue #30: assert_raises should return the exception instance
Author: jpellerin
Created: 2011-12-14 15:56:02+00:00
State: open

I see that nose.tools gets assert_raises from the unittest implementation, but I'd like to suggest
overriding that with a more useful function that will return the instance so that you can verify more
information about it:

def assert_raises(exception, callable, *args, **kw):
try:
callable(*args, **kw)
except exception, e:
return e
else:
if hasattr(exception, '__name__'):
name = exception.__name__
else:
name = ' or '.join([e.__name__ for e in exception])
assert False, '%s() did not raise %s' % (callable.__name__, name)

Usage:
def test_foo():
e = assert_raises(EnvironmentError, open, '/tmp/notfound')
assert e.errno == errno.ENOENT

Of course this is backwards compatible if you don't use the return value.

Google Code Info:
Issue #: 196
Author: m...@matt-good.net
Created On: 2008-06-27T01:21:11.000Z
Closed On: