I need to test my python code for an exception but I can't seem to get the right statement to run.
When I test my production code I can see that the exception is thrown but when I test for this exception in my unit test file I get this:
testincorrectparam (unittestearnings.TestEarningsArgs)
should raise error when param not an integer ... "3v" is not a valid argument. i should be an integer
ERROR
I read through similar questions on SO but I just don't manage to find a satisfactory answer for my problem..
The production code looks like this:
class Earnings():
def printd(i=0):
try:
i = int(i)
except Exception:
print('"' + i + '"' + " is not a valid argument. i should be an integer")
sys.exit(0)
And the unit test code is this:
from earnings import Earnings
import unittest
class TestEarningsArgs(unittest.TestCase):
def testincorrectparam(self):
'''should raise error when param not an integer'''
e = Earnings()
value = "3v"
self.assertRaises(Exception, e.printd(i=value))
if __name__ == "__main__":
unittest.main()
Really appreciate any help.
printd()doesn't take a "param" named parameter, so either your test or code is incorrect there as well.