I am new to unit testing, and I am trying to test a function that adds two numbers. THe program works fine if I want to test for the right result, but when I want to make the function fail the test, I get an AssertionError although I'm using try/except to catch that exception. I don't know what I'm doing wrong. Would someone please point that out?
import unittest
import sumXY
from random import randint
class Test(unittest.TestCase):
def test_add(self):
for a in range(1,5):
x = 2
y = 3
z = 6
try:
self.assertEqual(sumXY.sum(x,y), z)
print "%d + %d = %d" % (x, y, z) + " -> PASSED"
except:
print "%d + %d = %d" % (x, y, z) + " -> FAILED"
pass
if __name__ == '__main__':
unittest.main()
Output:
AssertionError: 5 != 6
I would like the output to be: 2 + 3 = 6 -> FAILED
exceptis nested inside thetryblock. Move theexceptto be on the same level astry.ctrl/command+kto mark it as a code block. See stackoverflow.com/editing-help for more info.AssertionErrors! They exist so that theunittestmodule knows whether the program failed or not. Also you didn't post the output you got.