解决双击运行:使用pyinstaller打包python+pytest+allure生成的index.html,allure报告显示loding问题
【问题描述】****:单独在文件中打开allure生成的index.html报告时显示为loading,如下图
【问题定位】****:其实在allure-report下index.html文件是不能直接打开的,出现页面都是loading的情况,这是因为直接allure报告用浏览器是打不开的,需要用allure命令渲染之后打开才能展示效果。
【解决方法】:
一、在操作报告文件夹解决
【方法1】:终端命令运行: allure open 或 allure serve
allure使用了两种方式来渲染页面。分别是 allure open 和 allure serve。
allure open:用于在本地渲染和查看结果;
allure serve:用于在本地渲染后对外展示结果。
这里我们使用allure open。运行命令:allure open allure-report目录(注:allure-report目录为index.html文件的所在目录位置,不需要带index.html)
注意:我这里使用了绝对路径,因为有两个allure-report文件夹,一个是架构生成,一个是使用pyinstaller打包生成。这里实现的是由架构生成双击运行allure报告用浏览器是打开的方法
cd F:\web_AutoTest\allure-report
allure open F:\web_AutoTest\allure-report\
【方法2】:编写.bat脚本,双击直接打开(该脚本要和index.html放在同一个目录下)
注:这里也可以编写默认用谷歌浏览器打开的bat脚本
二、修改代码解决使用pyinstaller打包python+pytest+allure生成的index.html,执行完测试用例,自动打开测试报告
【解决方法】:修改测试套件run_suite.py文件中的main函数,如图
import os
import time
import pytest
from allure_pytest import plugin as allure_plugin
if __name__ == "__main__":
args = ["-v", "-s", "--alluredir=report/allure", "--clean-alluredir"]
pytest.main(args=args, plugins=[allure_plugin])
time.sleep(2)
# 生成 Allure 报告
allure_report_dir = "report"
os.system(f"allure generate {allure_report_dir} --clean")
time.sleep(5)
os.system("allure open F:/web_AutoTest/allure-report/") # 运行完直接打开报告
"""
# 未使用pyinstaller打包工具前的代码:
pytest.main()
os.system("allure generate report --clean")
"""
修改完main函数代码,需要手动删除之前使用pyinstaller打包生成的“build”、“dist”文件夹和run_suite.spec文件,在pycharm的Terminal执行下方打包命令
【如图执行方式】
【执行代码】
pyinstaller --add-data "allure-report:allure-report" --add-data "./testcases;testcases" --add-data "./base/*.py;base" --add-data "./common/*.py;common" --add-data "./page/*.py;page" --add-data "./config.py;." --add-data "./data/login_data.json;data" --hidden-import "selenium" --hidden-import "selenium.webdriver" --hidden-import "selenium.webdriver.common" --hidden-import "selenium.webdriver.common.by" --hidden-import "logging.handlers" --hidden-import "utils" run_suite.py
执行完成后,看到如图所示的“successfully”,就是打包成了
在自动化测试脚本的文件中能看到新生成的文件夹“dist”,双击运行,dist\run_suite文件下的run_suite.exe文件
执行完所有测试用例后,谷歌浏览器自动打开测试报告
参考链接:https://blog.csdn.net/y2Candice/article/details/131883685