前言
在我们自动化测试过程中,我们最终肯定会生成一个测试报告,测试报告可能为一个txt、xml、json文件、Excel或者HTML报告,大家基本上都偏向于HTML报告,pytest 提供了pytest-html和allure的HTML报告。本章节我们讲解使用pytest-html
插件生成HTML报告。
pytest-html
用法
说明:pytest-html是pytest插件生成一个HTML测试报告。
安装
- 当前环境:
python 3.7.0
- 前提条件:
pytest (!=6.0.0,>=5.0)
、python > 3.0
- 安装方式:
pip install pytest-html==2.1.1
安装指定版本,3.0以上有兼容性问题,目前已经停止更新 - 查看版本号:
pip show pytest-html
使用
- 使用方式:添加命令行参数
--html=report.html
和--html=report.html --self-contained-html
- 查看示例:
test_htm.py
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time :2021/7/11 19:24
# @Author : king
# @File :test_html.py
# @Software :PyCharm
# @blog :https://blog.youkuaiyun.com/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
import logging
def test_01():
logging.info("我是 test_01 测试用例")
assert True
def test_02():
logging.info("我是 test_02 测试用例")
assert True
def test_03():
logging.info("我是 test_03 测试用例")
assert True
if __name__ == '__main__':
pytest.main()
在命令行窗口输入: pytest --html=test_report.html test_html.py
, 查看结果,在当前目录生成一个test_report.html的测试报告,在项目目录查看还有一个 assets
的样式文件目录
打开报告,可以看见一个HTML报告已经生成
我们删除项目目录下面的test_report.html
和assets
的样式文件目录,再次执行pytest --html=test_report.html --self-contained-html test_html.py
,查看结果:只看见 test_report.html
文件,未看见样式目录
结论:建议使用 --html=report.html --self-contained-html
方式生成报告,方便后期要通过邮件发送报告
修改pytest-html报告为中文
修改后样式:
修改源码位置
../Lib/site-packages/pytest_html/plugin.py
文件:
469行
summary = [
html.p(f"总共 {
numtests} 个测试用例运行时间 {
suite_time_delta:.2f} 秒. "),
html.p(
"可以通过选择筛选项筛选测试结果。",
class_="filter",
hidden="true",
),