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 ...
You can use a
with self.assertRaises(ExceptionType)
context manager to catch the exception. According to the assertRaises manual, you can then look at the exception after thewith
block: it appears to still be in scope if you give it a name with theas <name>
syntax:Source: docs.python.org
So your code would become:
PS: I wasn't aware of this, but the
with
statement does not introduce a new scope in Python. Variables defined insidewith
are in the same scope as thewith
statement itself. See https://stackoverflow.com/a/45100308/3216427 to this specific point and, for what actually creates scopes, https://stackoverflow.com/a/52992404/3216427.