Allure 趋势图不显示
一、趋势图不显示
allure报告,通常我们是结合jenkins使用,在jenkins中可以看到报告的趋势图,如下图所示
但是,在非CI环境下我们会发现,趋势图变成空白了
如何显示趋势图?首先需要了解,allure报告的产生原理
二、allure报告产生原理
1.1、报告生成步骤
(1)生成xml文件
(2)xml转为html文件
通常情况下,我们会运行如下命令:
pytest -sv --alluredir report
此时,会在项目文件夹的report文件夹下生成一些json文件,如图:
1.2、xml转html
1.https://bintray.com/qameta/generic/allure2 下载 allure-2.6.0.zip
2.解压缩到一个目录(不经常动的目录)
3.将压缩包内的 bin 目录配置到 path 系统环境变量
4.在命令行中敲 allure 命令,如果提示有这个命令,即为成功
使用步骤
在保证项目中的 report 目录下有 xml 文件的时候,执行以下步骤。
1.进入 report 上级目录执行命令
allure generate report/ -o report/html --clean
2.report 目录下会生成 html 文件夹,html 下会有一个 index.html ,右键用浏览器打开即可。
1.3 参数和命令详解
应用场景
修改 xml 所在的目录名称和 index.html 所在的目录名称
疑问和解答
- addopts = -s --alluredir report 中的 --alluredir report 是什么意思?
–alluredir 后面的 report 为 xml 输出的目录名
如果希望目录名叫 result 那么可以将命令行参数改为 --alluredir result - allure generate report/ -o report/html --clean 是什么意思?
report/ 表示 xml 所在的目录
-o 表示 output 输出
report/html 表示将 index.html 报告生成到哪个文件夹
三、趋势图产生原理
趋势图的产生原理,我们从jenkins中分析
1、打开目录中的.jenkins/jobs/test (test是Jenkins上的任务名称)
2、builds
构建历史都在此文件夹中
3、进去每次构建的文件夹即可看到每次的构建结果
4、archive
文件夹存放的就是每次生成的allure报告压缩包
5、解压后,找到history-trend.json
这个json里面存放的就是每次的构建结果,看名字就能得知历史趋势
6、分析history-trend.json
文件的规律
1.整体的文件里面最外层是一个"列表"
2.“列表"里面嵌套的是每次构建的历史,是一个"字典”
3.每一个"字典"里面又包含
buildOrder
:构建次数reportUrl
:报告的urlreportName
:报告名称data
:报告执行结果
7、再回过头看下我们自己生成的报告中的history-trend.json
1)使用命令allure generate test_allure_reports -o allure-reports --clean
将报告转成一个静态工程
2)找到history-trend.json
3)history-trend.json
文件里面数据只有data
,并没有构建次数、报告url和报告名称
8、总结
1、每次生成报告的时候需要在history-trend.json文件更新之前运行的结果
2、并且要在history-trend.json文件中的每次生成报告的时候添加 构建次数和报告url
3、添加构建次数是为了使得趋势图能够按照顺序展示
4、添加报告url是为了使得点击趋势图可以进行跳转,查看历史报告
四、代码部分
了解完原理后,就可以进行编码了。
import os import json import ast # 需要执行的测试脚本的路径 test_path = "F://ttx_ofs_autoapi/testCases/testOpenApi/test_brand_create" # 需要生成的xml的路径 allure_xml_path = "F://ttx_ofs_autoapi//allure_xml" # 需要生成测试报告的路径 allure_path = "F://ttx_ofs_autoapi//allure_report" # 获取下一个文件夹的名称,以及最近一个趋势的数据 def get_dir(): print("allure_path",allure_path) # 判断之前是否生成过报告 first_path = "F://ttx_ofs_autoapi//allure_report/1" if os.path.exists(first_path): all_result_dir = os.listdir(allure_path) # 这个地方要注意,如果是mac,listdir获取到一个.DS_store的文件,使用下方的sort会报错,因而要先all_result_dir中将它remove掉 all_result_dir.sort(key = int) # 取出最近一次执行的历史趋势的json history_file = os.path.join(allure_path,str(int(all_result_dir[-1])),'widgets','history-trend.json') with open(history_file) as f: data = f.read() # 将下一次要生成的文件夹序号以及最新的历史趋势数据返回 return int(all_result_dir[-1])+1,data else: # 如果之前没有生成过,就创建一个文件夹 os.makedirs(os.path.join(allure_path,'1')) return 1,None # 获取最新生成的趋势数据,这个数据里其实只有本次的结果,没有历史的结果 def update_new_single(buildOrder): new_single_file = os.path.join(allure_path, str(buildOrder), 'widgets', 'history-trend.json') with open(new_single_file,'r+') as f: # data1 = f.read() data = json.load(f) # 写入本次是第几次执行、测试报告的路径 data[0]["buildOrder"] = int(buildOrder) data[0]['reportUrl'] = f'http://localhost:63343/ttx_ofs_autoapi/allure_report/{str(buildOrder)}/index.html' with open(new_single_file,'w+') as f: json.dump(data,f) # 重写新生成的history-trend.json文件,用历史+本次=最新 def update_file(buildOrder): old_data = os.path.join(allure_path,str(int(buildOrder)-1),'widgets','history-trend.json') new_data = os.path.join(allure_path,str(int(buildOrder)),'widgets','history-trend.json') with open(old_data) as f: old = json.load(f) dict = [] for i in range(len(old)): dict.append(old[i]) print(dict) with open(new_data) as f: r = f.read() new_list = ast.literal_eval(r) for i in range(len(dict)): new_list.append(dict[i]) with open(new_data,'w') as f: json.dump(new_list,f) # 调用 def test_allure(): print(allure_path) buildOrder ,old_data = get_dir() # 先使用command生成xml文件 command = f'pytest {test_path} -s --alluredir={os.path.join(allure_xml_path,str(buildOrder))}' os.system(command) # 再使用command1由xml生成json文件的测试报告 command1 = f'allure generate {os.path.join(allure_xml_path,str(buildOrder))} -o {os.path.join(allure_path,str(buildOrder))} --clean' print(command1) os.system(command1) update_new_single(buildOrder) if buildOrder != 1: update_file(buildOrder)
五、趋势图出现loading
当完成一切操作后,可能会出现allure报告出现loading的情况,如图:
解决方案:在pycharm中找到index.html文件,右击选择浏览器打开,即可恢复正常
参考资料:
https://blog.csdn.net/RoninYang/article/details/111245351
https://www.cnblogs.com/ying1761/p/15792346.html
https://blog.csdn.net/weixin_44999591/article/details/120151140