【学习记录】Python使用HTMLTestrunner生成测试报告

时间:2023-01-14 18:34:46

下载HTMLTestRunner:  http://tungwaiyip.info/software/HTMLTestRunner.html, .py将文件放到Python的安装目录下Lib文件下的根目录,默认的是Python2.0,我已经上传Python3.0 和2.0到文件,可以直接下载

import  unittest
from HTMLTestRunner import HTMLTestRunner
import  time

test_dir='./'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')

if __name__ == '__main__':
        # runner = unittest.TextTestRunner()
        # print(discover)
        # runner.run(discover)
        #存放报告的文件名字
        report_dir="../test_report"
        #通过时间给文件命名
        now = time.strftime("%Y-%m-%d %H_%M_%S")
        #完整的文件名
        report_name =report_dir+'/'+now+' result.html'

        # 生成报告
        with open(report_name,'wb') as f:
                runner = HTMLTestRunner(stream=f, title='Test Report', description='test case result')
                runner.run(discover)

        f.close()