Alex Rivera | Logout

How do you test that a Python function throws an exception?

Asked 2008-09-24T20:00:35.597
1277

How does one write a unit test that fails only if a function doesn't throw an expected exception?

Edit
Report

1 Answer

1047

Use TestCase.assertRaises from the unittest module, for example:

import mymod

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(SomeCoolException, mymod.myfunc)
answered 2008-09-24T20:02:29.600

Your Answer