Allure趋势图本地实现
本地无法存储allure处理历史趋势数据不展示,是因为报告中
widgets
文件中history-trend.json
每次都会被更新,所以需要将前几次的数据存下来,放到一个json中,每次更新,并且在更新的过程中加上构建的次数,与每次的html索引(网址),即可得到趋势对比图
注意:网址路径并不是临时生成的展示路径,而是先尝试放入你想存的地方(尝试之后记得删除),再通过html文件打开后的地址命名格式!!!
1.第一步:对每次生成的报告进行储存
# 先进入目标文件夹
command = 'cd D:/project_pycharm/allure_test2/testcase'
os.system(command)
# 生成Allure报告
command = f'pytest ./test_allure_attach.py --alluredir ./allure-results'
os.system(command)
2.第二步:获取历史数据和构建次数
def get_dirname():
hostory_file = os.path.join(ALLURE_PLUS_DIR, "history.json")
if os.path.exists(hostory_file):
with open(hostory_file) as f:
li = eval(f.read())
# 根据构建次数进行排序,从大到小
li.sort(key=lambda x: x['buildOrder'], reverse=True)
# 返回下一次的构建次数,所以要在排序后的历史数据中的buildOrder+reports
return li[0]["buildOrder"]+1, li
else:
# 首次进行生成报告,肯定会进到这一步,先创建history.json,然后返回构建次数1(代表首次)
with open(hostory_file, "w") as f:
pass
return 1, None
3.第三步:更新备份历史数据history.json
和每个报告中的history-trend.json
def update_trend_data(dirname, old_data: list):
"""
dirname:构建次数
old_data:备份的数据
update_trend_data(get_dirname())
"""
allure_report_path = 'allure-report/widgets'
WIDGETS_DIR = allure_report_path
# 在reports文件夹下面创建相对应的构建次数文件夹
folder_path = f"./reports/{dirname}"
# os.makedirs(folder_path)
# 将allure-report中的所有文件复制到对应构建次数的文件夹中
# 定义源文件夹路径
source_folder = "./allure-report"
# 定义目标文件夹路径
target_folder = folder_path
# 复制源文件夹中的所有文件和子文件夹到目标文件夹
shutil.copytree(source_folder, target_folder)
# 读取最新生成的history-trend.json数据
with open(os.path.join(WIDGETS_DIR, "history-trend.json")) as f:
data = f.read()
new_data = eval(data)
if old_data is not None:
new_data[0]["buildOrder"] = old_data[0]["buildOrder"]+1
else:
old_data = []
new_data[0]["buildOrder"] = 1
# 给最新生成的数据添加reportUrl key,reportUrl要根据自己的实际情况更改
new_data[0]["reportUrl"] = f"http://localhost:63342/allure_test2/testcase/reports/{dirname}/index.html"
# 把最新的数据,插入到备份数据列表首位
old_data.insert(0, new_data[0])
# 把所有生成的报告中的history-trend.json都更新成新备份的数据old_data,这样的话,点击历史趋势图就可以实现新老报告切换
for i in range(1, dirname+1):
with open(os.path.join(ALLURE_PLUS_DIR, f"{str(i)}/widgets/history-trend.json"), "w+") as f:
f.write(json.dumps(old_data))
with open(os.path.join(allure_report_path, "history-trend.json"), "w+") as f:
f.write(json.dumps(old_data))
# 把数据备份到history.json
hostory_file = os.path.join(ALLURE_PLUS_DIR, "history.json")
with open(hostory_file, "w+") as f:
f.write(json.dumps(old_data))
return old_data, new_data[0]["reportUrl"]
4.第四步:调用顺序(需要使用os.system
进行执行命令行)
# 首先进入目标目录
command = 'cd D:/project_pycharm/allure_test2/testcase'
os.system(command)
# 生成Allure报告
command = f'pytest ./test_allure_attach.py --alluredir ./allure-results'
os.system(command)
# ALLURE_PLUS_DIR 是存放要生成的报告
ALLURE_PLUS_DIR = r"./reports"
# 先调用get_dirname(),获取到这次需要构建的次数和json信息
# buildOrder 是表示以构建次数为文件夹名称
buildOrder, old_data = get_dirname()
# 再执行命令行
# 对生成的Allure报告进行进一步演进(生成一个相对独立的报告静态工程)
command = "allure generate --clean "
os.system(command)
# 执行完毕后再调用update_trend_data()
all_data, reportUrl = update_trend_data(buildOrder, old_data)
# 打开报告
# open_command = "allure open"
# os.system(open_command)
5.完整例子
代码
部分参数需要根据实际情况进行修改
import os
import json
import shutil
def get_dirname():
hostory_file = os.path.join(ALLURE_PLUS_DIR, "history.json")
if os.path.exists(hostory_file):
with open(hostory_file) as f:
li = eval(f.read())
# 根据构建次数进行排序,从大到小
li.sort(key=lambda x: x['buildOrder'], reverse=True)
# 返回下一次的构建次数,所以要在排序后的历史数据中的buildOrder+reports
return li[0]["buildOrder"]+1, li
else:
# 首次进行生成报告,肯定会进到这一步,先创建history.json,然后返回构建次数1(代表首次)
with open(hostory_file, "w") as f:
pass
return 1, None
def update_trend_data(dirname, old_data: list):
"""
dirname:构建次数
old_data:备份的数据
update_trend_data(get_dirname())
"""
allure_report_path = 'allure-report/widgets'
WIDGETS_DIR = allure_report_path
# 在reports文件夹下面创建相对应的构建次数文件夹
folder_path = f"./reports/{dirname}"
# os.makedirs(folder_path)
# 将allure-report中的所有文件复制到对应构建次数的文件夹中
# 定义源文件夹路径
source_folder = "./allure-report"
# 定义目标文件夹路径
target_folder = folder_path
# 复制源文件夹中的所有文件和子文件夹到目标文件夹
shutil.copytree(source_folder, target_folder)
# 读取最新生成的history-trend.json数据
with open(os.path.join(WIDGETS_DIR, "history-trend.json")) as f:
data = f.read()
new_data = eval(data)
if old_data is not None:
new_data[0]["buildOrder"] = old_data[0]["buildOrder"]+1
else:
old_data = []
new_data[0]["buildOrder"] = 1
# 给最新生成的数据添加reportUrl key,reportUrl要根据自己的实际情况更改
new_data[0]["reportUrl"] = f"http://localhost:63342/allure_test2/testcase/reports/{dirname}/index.html"
# 把最新的数据,插入到备份数据列表首位
old_data.insert(0, new_data[0])
# 把所有生成的报告中的history-trend.json都更新成新备份的数据old_data,这样的话,点击历史趋势图就可以实现新老报告切换
for i in range(1, dirname+1):
with open(os.path.join(ALLURE_PLUS_DIR, f"{str(i)}/widgets/history-trend.json"), "w+") as f:
f.write(json.dumps(old_data))
with open(os.path.join(allure_report_path, "history-trend.json"), "w+") as f:
f.write(json.dumps(old_data))
# 把数据备份到history.json
hostory_file = os.path.join(ALLURE_PLUS_DIR, "history.json")
with open(hostory_file, "w+") as f:
f.write(json.dumps(old_data))
return old_data, new_data[0]["reportUrl"]
command = 'cd D:/project_pycharm/allure_test2/testcase'
os.system(command)
# 生成Allure报告
command = f'pytest ./test_allure_attach.py --alluredir ./allure-results'
os.system(command)
ALLURE_PLUS_DIR = r"./reports"
# 先调用get_dirname(),获取到这次需要构建的次数和json信息
buildOrder, old_data = get_dirname()
# 再执行命令行
# 对生成的Allure报告进行进一步演进(生成一个相对独立的报告静态工程)
# ALLURE_PLUS_DIR 是存放要生成的报告
# buildOrder 是表示以构建次数为文件夹名称
command = "allure generate --clean "
os.system(command)
# 执行完毕后再调用update_trend_data()
all_data, reportUrl = update_trend_data(buildOrder, old_data)
# 打开报告
# open_command = "allure open"
# os.system(open_command)
目录结构
其中
reports
中存储的就是之前报告,是通过上述代码逻辑存储下来的。
allure-results
是运行结果,allure-report
是系统生成的报告
test_allure_attach.py
用来存放用例上述代码放在以下目录中的
local_compare.py
文件中最后运行
local_compare.py
,即可得到最后的可视化对比
同时,点击可视化图中的不同区域,可实现不同测试测试报告之间的跳转
注意:第一次运行代码时,将reports中的内容清空,以免因为其他数据影响程序正常运行!!!