pytest-html
# pytest插件生成一个HTML测试报告。 pip install pytest-html
使用
# 添加命令行参数 --html=report.html 和 --html=report.html --self-contained-html if __name__ == "__main__": pytest.main(["test_debug.py", # 测试用例 "--html=../report/report0414.html", # 生成测试报告 生成assert存放的css文件和html文件 "--self-contained-html", # 把css样式合并到html里 仅生成html文件
])
# 运行中的日志显示ASCII颜色 if __name__ == "__main__": pytest.main(["test_debug.py", # 测试用例 "-v", # -v 显示详细信息, -q 不输出环境信息, -s 显示程序中的打印和日志 "--html=../report/report0414.html", # 生成测试报告 "--self-contained-html", # 把css样式合并到html里 "--color=yes", # pytest 写入输出颜色 ])
Allure
手动安装
配置环境变量
解压后将bin目录添加到path 并执行目录下 allure.bat 文件
pytest需要安装插件
# 安装依赖包 pip install allure-pytest
使用
if __name__ == "__main__": pytest.main(["test.py::Test", # "运行文件名"::"类名"::"方法名" "--alluredir", # 创建allure报告的路径 "../report/allure/temp_jsonreport", # allure生成的报告是json格式的,需要再转化成html格式的,所以会自动生成一个temp_jsonreport文件 "--clean-alluredir", # 先清空再生成新的测试报告 "--allure-no-capture", # 不添加pytest捕捉的logging/stdout/stderr到测试报告中 "-v", # 日志打印 -q仅报告 -s仅控制台, -v 报告及控制台 "--color=yes", # pytest 写入输出颜色 "-n 10", # 不使用则注释掉,xdist多进程并发 -n后+num '-W', 'ignore:Module already imported:pytest.PytestWarning', # 忽略错误 # "-p", "no:cacheprovider" # 禁用插件,缓存是通过cacheprovider插件 ])
# -o是执行 --clean是清除之前生成的报告 os.system(f"allure generate ../report/allure/temp_jsonreport -o ../report/allure/html --clean") # 自动在浏览器中打开allure测试报告 os.system(f"allure open {os.path.join(os.path.abspath('..'), 'report/allure/html')}")
<Tips:
--clean-alluredir:如果已经存在报告,那就先清空,然后再生成新的测试报告;
--allure-no-capture:不添加pytest捕捉的logging/stdout/stderr到测试报告中;
>
allure报告结构
常用特性
使用方法 | 参数值 | 参数说明 |
---|---|---|
@allure.epic() | epic描述 | 敏捷里面的概念,定义史诗,往下是feature |
@allure.feature() | 模块名称 | 功能点的描述,往下是story |
@allure.story() | 用户故事 | 用户故事,往下是title |
@allure.title(用例的标题) | 用例的标题 | 重命名html报告名称 |
@allure.step() | 操作步骤 | 测试用例的步骤 |
@allure.testcase() | 测试用例的链接地址 | 对应功能测试用例系统里面的case |
@allure.issue() | 缺陷 | 对应缺陷管理系统里面的链接 |
@allure.description() | 用例描述 | 测试用例的描述 |
@allure.severity() | 用例等级 | blocker,critical,normal,minor,trivial |
@allure.link() | 链接 | 定义一个链接,在测试报告展现 |
@allure.attachment() | 附件 | 报告添加附件 |
@allure.epic("模块") #敏捷里面的概念,定义史诗,相当于module级的标签 @allure.feature("功能") #功能点的描述,可以理解成模块,相当于class级的标签 @allure.story("场景") #故事,可以理解为场景,相当于method级的标签

# -*- coding: UTF-8 -*- import sys import os import json import time import allure @allure.epic("测试脚本") @allure.feature("多线程测试") class TestPressure: """测试用例模块""" @allure.story("测试用例") def test_01_demo(self): logs.info(datetime.datetime.now()) logs.info(datetime.datetime.now()) @allure.story("测试用例") @allure.feature("跳过测试") @pytest.mark.skip def test_03_demo(self): logs.info(datetime.datetime.now()) logs.info(datetime.datetime.now()) @allure.story("测试用例") def test_05_demo(self): logs.info(datetime.datetime.now()) assert 1 == 2, "不相等" def test_07demo(self): logs.info(datetime.datetime.now()) assert 1 == 1, "不相等" if __name__ == "__main__": pytest.main(["test_pressure.py::TestPressure", # "运行文件名"::"类名"::"方法名" "--alluredir", # 创建allure报告的路径 "../report/allure/temp_jsonreport", # allure生成的报告是json格式的,需要再转化成html格式的,所以会自动生成一个temp_jsonreport文件 "-v", # 日志打印 -q仅报告 -s仅控制台, -v 报告及控制台 "--color=yes", # pytest 写入输出颜色 # "-n 10", # 不使用则注释掉,xdist多进程并发 -n后+num '-W', 'ignore:Module already imported:pytest.PytestWarning', # 忽略错误 # "-p", "no:cacheprovider" # 禁用插件,缓存是通过cacheprovider插件 ]) # -o是执行 --clean是清除之前生成的报告 os.system(f"allure generate ../report/allure/temp_jsonreport -o ../report/allure/html --clean") # 自动在浏览器中打开allure测试报告 os.system(f"allure open {os.path.join(os.path.abspath('..'), 'report/allure/html')}")
执行结果
指定标签运行
--allure-epics --allure-features --allure-stories
指定运行方式
"--allure-features=多线程测试", "--allure-features 多线程测试, 跳过测试",
运行结果
遇到的问题
1、在py文件中执行执行测试用例时,未生成测试报告
if __name__ == "__main__": pytest.main(["test_debug.py", # 测试用例 "-q", # -v 显示详细信息, -q 不输出环境信息, -s 显示程序中的打印和日志 "--html=../report/report0414.html", # 生成测试报告 "--self-contained-html", # 把css样式合并到html里 ])
方法一:
原因:pytest没有main函数,会根据目录执行复核pytest规则的测试用例
解决:将pycharm 的测试运行程序改为 unittest
2、使用 loguru 插件生成的日志,在生成html报告时 中文乱码
解决方案:将loguru 日志控制台输出关闭并添加logging输出到控制台
完整代码参考: https://www.cnblogs.com/phoenixy/p/16150119.html
# 删除以前添加的处理程序并停止向其接收器发送日志。 logs.remove(handler_id=None) # 清除之前的设置 # 集成loguru到控制台(即html报告) class PropogateHandler(logging.Handler): def emit(self, record): logging.getLogger(record.name).handle(record) logs.add(PropogateHandler(), format="{time:YYYY-MM-DD HH:mm:ss} | {message}")
3、allure 目录文件打开页面展示Loading和404
问题原因:allure生成的html测试报告无法直接在浏览器打开
解决方法:
在report目录使用命令 allure open 或allure serve
报告子目录执行
allure open html allure serve html # 信息不完整
报告目录执行
allure open allure\html allure serve allure\html # 信息不完整