pytest-html生成报告
Pytest-HTML是一个插件,它可以生成漂亮且容易阅读的HTML测试报告。
安装插件:
pip install pytest-html
运行测试并生成报告:
pytest --html=report.html
查看报告:
打开生成的HTML报告文件,可以查看测试结果的摘要、详细的测试用例执行信息、失败用例的堆栈跟踪等。
pytest-html添加图片
from pytest_html import extras
def test_add_img(extra):
def image_to_base64(image path):
import base64
""这个函数是将图片转化为base64数据“”
with open(image_path,"rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string,decode('utf-8')
# 添加图片文件
image_path ="/VoLumes/huace/Pytest/你好.jpg" # 图片路径
extra.append(extras.jpg(image_path)) # 添加图片
#添加base64格式的图片
base64_data = image_to_base64(image_path)
extra.append(extra.image(base64_data))
#运行文件
import pytest
pytest.main(['--html=report/report.html','--self-contained-html'])
pytest-html报告中乱码解决
标题乱码解决:
内容乱码解决:
allure-pytest生成测试报告
下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
Allure安装:
下载安装包,解压到指定路径,进入到allure的bin路径下,复制bin的完整路径,添加到系统环境变量的path之中
启动cmd,运行allure --version指令,如果在控制台显示有allure版本信息则表示安装成功
allure-pytest插件安装:
pip install allure-pytest
运行生成测试报告:
import pytest
pytest.main(['--alluredir','alure_result','--clean-alluredir'])#获取测试结果,并以allure的数据格式保存
import os
os.system('allure generate --clean ./allure_result -o ./allure_report')#通过allure的数据进行报告生成
allure-pytest的乱码解决
1.将pycharm配置到环境变量中,重启pycharm
2.修改pycharm编码