Unit Testing in Python3
Unit testing functionality is built into Python3. Following is a very simple example:
def Arithmetic(a) return a * a
Save this file as mycode.py. Save the following code as mycode_unittest.py
import unittest from mycode import Arithmetic class ArithmeticTest(unittest.TestCase): def setUp(self): self.arr1 = ((0,0),(1,1)) self.arr2 = ((1,2),(3,4)) def testCalculation(self): for (i,val) in self.arr1: self.assertEqual(Arithmetic(i), val) for (i,val) in self.arr2: self.assertNotEqual(Arithmetic(i), val) def tearDown(self): self.arr1 = None self.arr2 = None if __name__ == "__main__": unittest.main()
When you run the unit test code, you will not get any errors. Change assertEqual to assertNotEqual. Then you will get unit test failure.