HTMLTestRunner下载地址:
- python2:http://tungwaiyip.info/software/HTMLTestRunner.html
- python3:https://pan.baidu.com/s/1dEZQ0pz
将下载好的HTMLTestRunner.py文件放到python安装目录的Lib文件夹中
#coding=utf-8 import unittest import HTMLTestRunner import time def creatsuite(): testunit = unittest.TestSuite() test_address = ‘C:\\Users\\user\\Desktop\\python_study\\script‘ #定义discover方法的参数 discover = unittest.defaultTestLoader.discover( test_address, pattern=‘un_*.py‘, top_level_dir=None ) for test_suite in discover: for test_case in test_suite: testunit.addTests(test_case) return testunit if __name__ == ‘__main__‘: testtime = time.strftime(‘%Y%m%d_%H%M%S_‘) #获取当前时间 #报告的保存地址及报告名称 repo_add=‘C:/Users/user/Desktop/python_study/report/‘+testtime+‘result.html‘ fp = file(repo_add,‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=‘Test Report‘, description=‘Test Result‘) runner.run(creatsuite()) fp.close() #关闭报告文件
如果生成的测试报告名字相同,后生成的测试报告会覆盖前面的测试报告,为了保证报告不重名,在报告名字中加入时间戳
testtime = time.strftime(‘%Y%m%d_%H%M%S_‘) repo_add=‘C:/Users/user/Desktop/python_study/report/‘+testtime+‘result.html‘
输出的测试报告:
原文地址:https://www.cnblogs.com/hlbzzt/p/10002836.html
时间: 2024-09-29 10:21:19