How does one write a unittest that fails only if a function doesn’t throw an expected exception?

18 s
18

Use TestCase.assertRaises (or TestCase.failUnlessRaises) from the unittest module, for example:

import mymod

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(SomeCoolException, mymod.myfunc)

Leave a Reply

Your email address will not be published. Required fields are marked *