PythoncodeUnitTest&CodeCoverageCodeUnitTestTheunittestmoduleprovidesarichsetoftoolsforconstructingandrunningtests.Thissectiondemonstratesthatasmallsubsetofthetoolssufficetomeettheneedsofmostusers.Preparesourcecodetobetest:CodemyClass.py:#-*-coding:utf-8-*-classSrcCode:def__init__(self):passdefsum(self,x,y):returnx+ydefsub(self,x,y):returnx-ydefdev(self,x,y):returnx/yCreatetestcase:CodetestMyClass01.py#-*-coding:utf-8-*-importunittest#importcoveragefromSourceCodeimportmyClassclassmyTest01(unittest.TestCase):##__init__defsetUp(self):self.tClass=myClass.SrcCode()#tearedowncleandeftearDown(self):pass#Testcasefunctionmustusetestbegan,liketestabcdeftestsum(self):self.assertEqual(self.tClass.sum(3,2),5,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(4,2),2,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(3,1),3,'testsubfail')classmyTest02(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(self):passdeftestsum(self):self.assertEqual(self.tClass.sum(3,1),4,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(4,1),3,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(3,1),3,'testsubfail')classmyTest03(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(self):passdeftestsum(self):self.assertEqual(self.tClass.sum(3),4,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(4,1),3,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(3,1),3,'testsubfail')classmyTest04(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(self):passdeftestsum(self):self.assertEqual(self.tClass.sum(),4,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(4,1),3,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(3,1),3,'testsubfail')if__name__=='__main__':unittest.main()#coverage.process_startup()Console:Findingfiles...done.Importingtestmodules...done.======================================================================ERROR:testsum(TestCase.testMyClass01.myTest03)----------------------------------------------------------------------Traceback(mostrecentcalllast):FileC:\Users\Administrator\workspace\UnitTest\TestCase\testMyClass01.py,line60,intestsumself.assertEqual(self.tClass.sum(3),4,'testsumfail')TypeError:sum()takesexactly3arguments(2given)======================================================================ERROR:testsum(TestCase.testMyClass01.myTest04)----------------------------------------------------------------------Traceback(mostrecentcalllast):FileC:\Users\Administrator\workspace\UnitTest\TestCase\testMyClass01.py,line80,intestsumself.assertEqual(self.tClass.sum(),4,'testsumfail')TypeError:sum()takesexactly3arguments(1given)----------------------------------------------------------------------Ran12testsin0.002sFAILED(errors=2)CodetestMyClass02.py#-*-coding:utf-8-*-importunittestfromSourceCodeimportmyClass#importcoverageclassmyTest02(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(self):passdeftestsum(self):self.assertEqual(self.tClass.sum(1,2),3,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(1,2),-1,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(2,0),2,'testsubfail')if__name__=='__main__':unittest.main()#coverage.process_startup()Console:E..======================================================================ERROR:testdev(__main__.myTest02)----------------------------------------------------------------------Traceback(mostrecentcalllast):FileC:\Users\Administrator\workspace\UnitTest\TestCase\testMyClass02.py,line15,intestdevself.assertEqual(self.tClass.dev(2,0),2,'testsubfail')FileC:\Users\Administrator\workspace\UnitTest\SourceCode\myClass.py,line14,indevreturnx/yZeroDivisionError:integerdivisionormodulobyzero----------------------------------------------------------------------Ran3testsin0.000sFAILED(errors=1)CodetestMyClass03.py#-*-coding:utf-8-*-importunittestfromSourceCodeimportmyClassimportcoverageclassmyTest02(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(self):passdeftestsum(self):self.assertEqual(self.tClass.sum(4,2),6,'testsumfail')deftestsub(self):self.assertEqual(self.tClass.sub(1,2),1,'testsubfail')deftestdev(self):self.assertEqual(self.tClass.dev(2,1),2,'testsubfail')if__name__=='__main__':unittest.main()Console:.F.======================================================================FAIL:testsub(__main__.myTest02)----------------------------------------------------------------------Traceback(mostrecentcalllast):FileC:\Users\Administrator\workspace\UnitTest\TestCase\testMyClass03.py,line13,intestsubself.assertEqual(self.tClass.sub(1,2),1,'testsubfail')AssertionError:testsubfail----------------------------------------------------------------------Ran3testsin0.001sFAILED(failures=1)CodetestMyClass04.py#-*-coding:utf-8-*-importunittestfromSourceCodeimportmyClass#importcoverageclassmyTest02(unittest.TestCase):defsetUp(self):self.tClass=myClass.SrcCode()deftearDown(se