一、unittest框架自带测试报告
适用于单条测试用例执行
当使用unittest框架,测试脚本执行结束,只需要导出结果即可 Export Test Results...
二、使用第三方模板生成
适用于测试套件的执行----->批量执行测试脚本
(1)复制第三方模板HTMLTestReportCN_New.py到项目文件夹
(2)生成测试套件---->suite
(3)确定HTML包含的文件名及存放目录-----> file_name
(4)with open(file_name, “wb”) as file:
file_name:html报告的路径+文件名
wb:write, byte 以二进制的格式写入
file:文件句柄
(5)实例化HTMLTestRunner类
对象名 = HTMLTestRunner(stream[ , title, description, tester])
stream:文件信息流--->文件句柄
title:报告的标题
description:一般写对于测试环境的描述信息(操作系统、浏览器……)
tester:测试人员
示例:
from Tools.HTMLTestReportCN_New import HTMLTestRunner
import unittest
import time
# 生成测试套件
suite = unittest.defaultTestLoader.discover("cases/", "ca*.py")
# 报告存放的路径
report_dir = "reports/"
now_time = time.strftime("%Y%m%d_%H%M%S")
# HTML报告的名字
report_name = report_dir + "XXX项目自动化测试报告_" + now_time + "_Report.html"
# 执行测试套件,生成测试报告
with open(report_name, 'wb') as file:
# 实例化HTMLTestRunner
runner = HTMLTestRunner(stream=file,
title="XXX网站自动化测试报告",
description="项目版本:2.3.4"
"selenium: 4.0.0"
"操作系统:win7"
"pthon: 35.0+"
"pycharm :社区版"
"浏览器: Chrome95.0 或Firefox60.0",
tester="lilgaage"
)
# 执行测试套件
runner.run(suite)