To test a function, where I raise an exception when the first argument is not of integer type:
def magicWithInteger(integerA):
if not isinstance(integerA, int):
raise TypeError("argument should be integer type")
I use unittest assertRaises AND assertEqual, so I can check if the function with wrong argument raises the TypeError AND if the TypeError actually spits out "argument should be integer type"
class test(unittest.TestCase):
def test_magicWithInteger(self):
self.assertRaises(TypeError, MagicWithInteger, "TEST")
try:
MagicWithInteger("TEST")
except TypeError as error:
self.assertEqual(str(error), "argument should be integer type")
It looks a bit silly to call the function twice, first to check if it raises the exception and second to check which TypeError exception?
After some research, I know it should be possible to do these two tests in one go with context manager, but I can not seem to make ends meet ...