I ran into same problem for my unit tests. mouad & Michael are correct, but for cleaner way to handle assertion for exception with consistent method.
class TestCase(unittest.TestCase):
def assertNoRaise(self, callableObj):
try:
callableObj()
except:
raise AssertionError('shouldn\'t raise an exception')
def assertAnyRaise(self, callableObj):
try:
callableObj()
except:
return
raise AssertionError('should raise an exception')
or seperate above class from inheritance, and do mixin with unittest.TestCase. either way would work
and a use case
class my_test(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_assertion(self):
self.assertAnyRaise(lambda: 1+'a')
self.assertNoRaise(lambda: 1+1)
please excuse my bad english.