I use following decorator inspired by one in nose:
def raises(exception, message):
@decorator
def decorate(func, *args, **kwargs):
name = func.__name__
try:
func(*args, **kwargs)
except exception, e:
if message is not None:
assert_equal(str(e), message)
except:
raise
else:
msg = "%s() did not raise %s" % (name, exception.__name__)
raise AssertionError(msg)
return decorate
So I can do something like:
class TestMyThing(TestCase):
@raises(ValueError, "T should not exceed 100, but has value 120")
def test(self):
some_code_that_provoke_exception()
The @decorator
is from decorator package. You can write decorator by hands if you don't want to introduce a dependency for some reason.