Hello sekitaka. I can't get rid of the feeling of being a python beginner forever.
Today I'll show you how to test that an Exception is raised in a python unit test.
def test_raise_exception(self):
with self.assertRaises(Exception):
raise Exception('hogehoge error') #Test code that expects Raise of Exception
At first, I tried to judge that the test was successful if I did self.fail ()
without try
and skipped to ʻexcept` before fail was executed, but fail in try was also set to except. I was caught and it wasn't the intended test.
No code
def test_raise_exception(self):
try:
raise Exception('hogehoge error') #Test code that expects Raise of Exception
self.fail("Failed because it was not excluded")
except Exception as e:
pass
Recommended Posts