# Lab 01: Python and Unit Test¶

## Unit Test in Python¶

Module: unittest.py

## Code Example¶

• calc.py
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 """ NAME: Zhiqiang Ren Discussion Session 1 CS440 / CS640 Artificial Intelligence """ class Calculator(object): """A simple calculator for integers.""" def __init__(self, x): self.x = x def add(self, x): ret = self.x + x self.x = x return ret def sub(self, x): ret = self.x - x self.x = x return ret def mul(self, x): ret = self.x * x self.x = x return ret def div(self, x): ret = self.x / x self.x = x return ret 
• testcalc1.py
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 """ NAME: Zhiqiang Ren Discussion Session 1 CS440 / CS640 Artificial Intelligence """ from calc import Calculator import unittest class Calc0TestCaseAdd(unittest.TestCase): def runTest(self): calc = Calculator(0) assert calc.add(1) == 1, "addition is wrong" class Calc0TestCaseSub(unittest.TestCase): def runTest(self): calc = Calculator(0) assert calc.sub(1) == -1, "substraction is wrong" def getTestSuite(): suite = unittest.TestSuite() suite.addTest(Calc0TestCaseAdd()) suite.addTest(Calc0TestCaseSub()) return suite def main(): runner = unittest.TextTestRunner() runner.run(getTestSuite()) if __name__ == '__main__': main() 
• testcalc2.py
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 """ NAME: Zhiqiang Ren Discussion Session 1 CS440 / CS640 Artificial Intelligence """ from calc import Calculator import unittest class Calc0TestCase(unittest.TestCase): def setUp(self): self.calc = Calculator(0) def tearDown(self): self.calc = None def testAdd1(self): assert self.calc.add(1) == 1, "addition is wrong" def testAdd2(self): assert self.calc.add(2) == 2, "addition is wrong" def testSub(self): assert self.calc.sub(1) == -1, "substraction is wrong" def getTestSuite(): suite = unittest.TestSuite() suite.addTest(Calc0TestCase("testAdd1")) suite.addTest(Calc0TestCase("testAdd2")) suite.addTest(Calc0TestCase("testSub")) return suite def main(): runner = unittest.TextTestRunner() runner.run(getTestSuite()) if __name__ == '__main__': main() 
• testcalc3.py
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 """ NAME: Zhiqiang Ren Discussion Session 1 CS440 / CS640 Artificial Intelligence """ from calc import Calculator import unittest class Calc0TestCase(unittest.TestCase): def setUp(self): self.calc = Calculator(0) def tearDown(self): self.calc = None def testAdd1(self): assert self.calc.add(1) == 1, "addition is wrong" def testAdd2(self): assert self.calc.add(2) == 2, "addition is wrong" def testSub(self): assert self.calc.sub(1) == -1, "substraction is wrong" class Calc1TestCase(unittest.TestCase): def setUp(self): self.calc = Calculator(1) def tearDown(self): self.calc = None def testAdd1(self): assert self.calc.add(1) == 2, "addition is wrong" def testAdd2(self): assert self.calc.add(2) == 3, "addition is wrong" def testSub(self): assert self.calc.sub(1) == 0, "substraction is wrong" def getTestSuite(): suite1 = unittest.makeSuite(Calc0TestCase, "test") suite2 = unittest.makeSuite(Calc1TestCase, "test") suite = unittest.TestSuite() suite.addTest(suite1) suite.addTest(suite2) return suite def main(): runner = unittest.TextTestRunner() runner.run(getTestSuite()) if __name__ == '__main__': main() 
• testcalc4.py
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 """ NAME: Zhiqiang Ren Discussion Session 1 CS440 / CS640 Artificial Intelligence """ from calc import Calculator import unittest class Calc0TestCase(unittest.TestCase): def setUp(self): self.calc = Calculator(0) def tearDown(self): self.calc = None def testAdd1(self): assert self.calc.add(1) == 1, "addition is wrong" def testAdd2(self): assert self.calc.add(2) == 2, "addition is wrong" def testSub(self): assert self.calc.sub(1) == -1, "substraction is wrong" class Calc1TestCase(unittest.TestCase): def setUp(self): self.calc = Calculator(1) def tearDown(self): self.calc = None def testAdd1(self): assert self.calc.add(1) == 2, "addition is wrong" def testAdd2(self): assert self.calc.add(2) == 3, "addition is wrong" def testSub(self): assert self.calc.sub(1) == 0, "substraction is wrong" def getTestSuite(): suite1 = unittest.makeSuite(Calc0TestCase, "test") suite2 = unittest.makeSuite(Calc1TestCase, "test") suite = unittest.TestSuite() suite.addTest(suite1) suite.addTest(suite2) return suite if __name__ == '__main__': unittest.main() # python testcalc4.py # python testcalc4.py Calc0TestCase # python testcalc4.py Calc1TestCase # python testcalc4.py getTestSuite # python testcalc4.py Calc0TestCase.testSub