Files
apiautotest/files/gen_allure.py
T

63 lines
2.8 KiB
Python

import subprocess
def generate_allure_report(**kwargs):
"""
通过allure生成html测试报告,并对报告进行美化
"""
allure_results_dir = kwargs.get("allure_results")
allure_report_dir = kwargs.get("allure_report")
# ----------------判断运行的平台,是linux还是windows,执行不同的allure命令----------------
cmd = f"{PlatformHandle().allure} generate {allure_results_dir} -o {allure_report_dir} --clean"
try:
# subprocess.run 会等待命令执行完成
result = subprocess.run(
cmd,
shell=True, # 允许字符串命令
check=True, # 出错会抛异常
stdout=subprocess.PIPE, # 捕获标准输出
stderr=subprocess.PIPE, # 捕获错误输出
text=True # 输出为字符串(而不是字节)
)
print(result.stdout) # 正常日志
if result.stderr:
print("⚠️ Allure 生成报告时有警告/错误:")
print(result.stderr)
except subprocess.CalledProcessError as e:
print("❌ Allure 报告生成失败!")
print("命令:", e.cmd)
print("返回码:", e.returncode)
print("错误输出:", e.stderr)
raise # 把异常抛出去,外层能感知失败
# ----------------美化allure测试报告 ------------------------------------------
allure_beautiful = AllureReportBeautiful(
allure_html_path=allure_report_dir, allure_results_path=allure_results_dir
)
# 设置报告窗口的标题
allure_beautiful.set_windows_title(new_title=kwargs.get("windows_title"))
# 修改Allure报告Overview的标题文案
allure_beautiful.set_report_name(new_name=kwargs.get("report_name"))
# 在allure-html报告中往widgets/environment.json中写入环境信息
allure_beautiful.set_report_env_on_html(env_info=kwargs.get("env_info"))
# ----------------压缩allure测试报告,方便后续发送压缩包------------------------------------------
allure_config_path = kwargs.get("allure_config_path") # 保存http_server.exe及双击打开Allure报告.bat的目录
copy_file(
src_file_path=os.path.join(allure_config_path, [i for i in os.listdir(allure_config_path) if i.endswith(".exe")][0]),
dest_dir_path=allure_report_dir,
)
copy_file(
src_file_path=os.path.join(allure_config_path, [i for i in os.listdir(allure_config_path) if i.endswith(".bat")][0]),
dest_dir_path=allure_report_dir,
)
attachment_path = kwargs.get("attachment_path") # allure报告压缩的路径,例如:report/allure_report.zip
zip_file(in_path=allure_report_dir, out_path=attachment_path)
return allure_report_dir, attachment_path