debugpythonCritical
How do you test that a Python function throws an exception?
Viewed 0 times
howfunctionthrowspythontestexceptionyouthat
Problem
How does one write a unit test that fails only if a function doesn't throw an expected exception?
Solution
Use
TestCase.assertRaises from the unittest module, for example:import mymod
class MyTestCase(unittest.TestCase):
def test1(self):
self.assertRaises(SomeCoolException, mymod.myfunc)Code Snippets
import mymod
class MyTestCase(unittest.TestCase):
def test1(self):
self.assertRaises(SomeCoolException, mymod.myfunc)Context
Stack Overflow Q#129507, score: 1046
Revisions (0)
No revisions yet.