增加Jenkins修改allure报告名称的方法,优化本地修改allure报告名称的方法

This commit is contained in:
wangjie 2025-01-07 19:03:58 +08:00
parent 4459b8e64f
commit 8da0e848ad
2 changed files with 58 additions and 10 deletions

View File

@ -12,8 +12,6 @@ import allure
import pytest
from common.models import AllureAttachmentType
from common.settings import ENV
from configs.env_config import EnvConfig
from configs.paths_config import TEMP_DIR, ALLURE_REPORT_DIR
@ -113,14 +111,16 @@ class AllureReportBeautiful:
@return:
"""
title_filepath = os.path.join(ALLURE_REPORT_DIR, "widgets", "summary.json")
# 读取summary.json中的json数据并改写reportName
with open(title_filepath, 'rb') as f:
# 加载json文件中的内容给params
# 检查summary.json文件是否存在
if not os.path.exists(title_filepath):
raise FileNotFoundError(f"修改报告名称时summary.json文件未找到 {title_filepath}")
# 读取summary.json中的内容
with open(title_filepath, 'r', encoding='utf-8') as f:
params = json.load(f)
# 修改内容
params['reportName'] = new_name
# 往summary.json中覆盖写入新的json数据
with open(title_filepath, 'w', encoding="utf-8") as f:
# 修改报告名称
params['reportName'] = new_name
# 将修改后的内容写回summary.json
with open(title_filepath, 'w', encoding='utf-8') as f:
json.dump(params, f, ensure_ascii=False, indent=4)
@staticmethod
@ -129,6 +129,9 @@ class AllureReportBeautiful:
在allure-results报告的根目录下生成一个写入了环境信息的文件environment.properties(注意不能放置中文否则会出现乱码)
@return:
"""
# 方法内导入防止其他文件引用allure_handle文件时引发循环导入问题
from common.settings import ENV
from configs.env_config import EnvConfig
# 需要写入的环境信息
allure_env = {
'OperatingEnvironment': ENV,
@ -165,4 +168,4 @@ class AllureReportBeautiful:
if __name__ == '__main__':
AllureReportBeautiful.set_report_name('测试报告1234')
AllureReportBeautiful.set_report_name('API自动化测试报告')

View File

@ -5,6 +5,10 @@
# @File : jenkins_handle.py
# @project : SensoroApiAutoTest
import os
import shutil
from configs.paths_config import OUT_FILES_DIR
from utils.allure_handle import AllureReportBeautiful
def get_env_from_jenkins(name, base=''):
@ -16,3 +20,44 @@ ProjectName = get_env_from_jenkins("JOB_NAME") # Jenkins构建项目名称
BUILD_URL = get_env_from_jenkins("BUILD_URL") # Jenkins构建项目URL
BUILD_NUMBER = get_env_from_jenkins("BUILD_NUMBER") # Jenkins构建编号
ALLURE_URL = BUILD_URL + 'allure/' # Jenkins构建的allure报告地址
JENKINS_HOME = get_env_from_jenkins("JENKINS_HOME") # Jenkins的主目录
def change_jenkins_allure_report_name():
"""从环境变量中读取报告名称并修改Allure报告名称此方法只针对Jenkins使用allure插件生成的报告"""
try:
# 从环境变量中读取报告名称
new_name = os.getenv('ALLURE_REPORT_NAME',
'Allure Report') # 如果环境变量中没有ALLURE_REPORT_NAME并且未传报告名称参数默认使用'Allure Report'
if not new_name:
raise ValueError("环境变量'ALLURE_REPORT_NAME'未设置或为空。")
print(f"使用Allure报告名称: {new_name}")
# 设置Allure报告名称
AllureReportBeautiful.set_report_name(new_name)
# 保存压缩文件目标路径(压缩文件将移动到这里)
zip_path = os.path.join(JENKINS_HOME, 'jobs', ProjectName, 'builds', BUILD_NUMBER, 'archive',
'allure-report.zip')
# 确保压缩文件不存在,如果存在先删除
if os.path.exists(zip_path):
os.remove(zip_path)
print(f"已删除现有zip文件{zip_path} ")
# 使用shutil.make_archive压缩整个Allure Report目录并移动到目标目录OUT_FILES_DIR参数的作用是保留原目录名称
shutil.make_archive(zip_path.replace('.zip', ''), 'zip', OUT_FILES_DIR, 'allure_report')
print(f"Allure报告压缩为: {zip_path}")
# 返回压缩后的文件路径
return zip_path
except Exception as e:
print(f"错误发生: {e}")
return None
if __name__ == '__main__':
change_jenkins_allure_report_name()