整体代码结构:
1开发写出完整单元代码
2测试编写单元测试用例代码
3主函数执行运行代码
4生成log结果显示(输出效果不好,不建议用)
5使用HTMLTestRunner实现html效果
具体代码如下:
1.单元代码例如myMath.py:
#定义一个类,实现加减乘除算法 #对该类中的加减乘除进行单元测试(加法实现对不对) import os class mymath(): def jia(self,a,b): return a+b def jian(self,a,b): return a-b def chengfa(self,a,b): return a*b def chufa(self,a,b): if b==0: return "被除数不能为o" else: return a/b if __name__ == ‘__main__‘: mm = mymath() # he = mm.jia(2, 3)
2.测试用例代码Unittest.py:
1 ‘‘‘ 2 使用Unitest框架测试mymath类中的加减乘除 3 实现步骤: 4 1导包(Unittest模块,mymath模块) 5 2创建一个单元测试类(继承自unittest.testcase) 6 3定义三个测试用例的方法: 7 def setUp() 8 def test_xx() 9 def tearDown() 10 ‘‘‘ 11 12 #导包,导模块 13 import os 14 import unittest 15 from myMath import mymath 16 17 #创建一个单元测试类(继承自unittest.testcase) 18 class myMathTest(unittest.TestCase): 19 20 #测试用例资源初始化方法 21 def setUp(self): 22 self.mm = mymath() 23 24 #测试用例的方法 25 def test_add_1(self): 26 ‘‘‘验证整形加法功能‘‘‘ 27 actulValue = self.mm.jia(2,1) 28 expectValue = 3 29 #断言 30 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 31 32 def test_add_2(self): 33 ‘‘‘验证字符串加法功能‘‘‘ 34 actulValue = self.mm.jia("abc","def") 35 expectValue = "abcdef" 36 #断言 37 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 38 39 def test_jian_1(self): 40 ‘‘‘验证整形减法功能‘‘‘ 41 actulValue = self.mm.jian(2,1) 42 expectValue = 1 43 #断言 44 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 45 46 def test_cheng_1(self): 47 ‘‘‘验证整形乘法功能‘‘‘ 48 actulValue = self.mm.chengfa(2,5) 49 expectValue = 10 50 #断言 51 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 52 53 def test_cheng2_1(self): 54 ‘‘‘验证字符串乘法功能‘‘‘ 55 actulValue = self.mm.chengfa("a",2) 56 expectValue = "aa" 57 #断言 58 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 59 60 def test_chu_1(self): 61 ‘‘‘验证整形除法功能‘‘‘ 62 actulValue = self.mm.chufa(4,0) 63 expectValue = 2 64 #断言 65 self.assertEqual(actulValue,expectValue,"预期结果和不一致") 66 67 #测试用例的资源释放 68 def tearDown(self): 69 pass 70 71 if __name__ == ‘__main__‘: 72 # unittest.main() 73 #直接使用discover 74 discover = unittest.defaultTestLoader.discover(r"../20210627/",pattern="Unittest.py") 75 #使用runner运行器运行测试集 76 with open(r"../20210627/re.txt","a",encoding="utf-8") as f: 77 runner = unittest.TextTestRunner(f,descriptions="用于测试math类的用例执行",verbosity=2) 78 runner.run(discover)
3.执行测试用例maintest.py:
1 ‘‘‘ 2 这是一个主文件,不是用来写用例,而是组织用例执行的 3 runner是unittest自带的,效果不好,使用第三方展示html格式 4 HTMLTestRunner模块:就是一个第三方 5 1下载HTMLTestRunner.py文件 6 2复制该文件到python安装目录下lib文件内 7 3导包:from HTMLTEstRunner import HTMLTestRunner 8 4代码实现: 9 discover = unittest.defaultTestLoader.discover(r"../20210627/",pattern="Unittest.py") 10 with open(r"../20210627/result.html","wb") as f: 11 runner = HTMLTestRunner(f,verbosity=2,title="单元测试报告",description="第一次运行结果") 12 runner.run(discover) 13 ‘‘‘ 14 import os 15 import time 16 import unittest 17 # from HTMLTestRunner3 18 from HTMLTestRunner import HTMLTestRunner 19 20 21 # import HTMLTestRunner3 22 23 #直接使用discover 24 discover = unittest.defaultTestLoader.discover(r"../20210627/",pattern="Unittest.py") 25 #使用runner运行器运行测试集 26 # with open(r"../20210627/re2.txt","a",encoding="utf-8") as f: 27 # runner = unittest.TextTestRunner(f,descriptions="用于测试math类的用例执行",verbosity=2) 28 # runner.run(discover) 29 30 #使用HTMLTestRunner运行器运行测试集合 31 # with open(r"../20210627/result.html","wb") as f: 32 # runner = HTMLTestRunner(f,verbosity=2,title="单元测试报告",description="第一次运行结果") 33 # runner.run(discover) 34 35 filename = time.strftime("%Y-%m-%d-%H-%M-%S")+".html" 36 path = os.path.dirname(__file__)+r"/" 37 filename = path+filename 38 print(filename) 39 40 with open(filename,"wb") as f: 41 runner = HTMLTestRunner(f,verbosity=2,title="单元测试报告",description="第一次运行结果") 42 runner.run(discover)
4.输出html格式报告:
5.配置HTMLTestRunner文件:
参考文档:https://blog.csdn.net/xiaojing0511/article/details/96716932?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_utm_term-0&spm=1001.2101.3001.4242