在执行完测试之后想要将测试结果通过邮件或者工作通信软件发送,需要在发送前进行结果的收集。
使用allure.py
执行完测试脚本之后,会在./report/allure_results
路径下生成Allure的测试过程数据,通过编写一个脚本来解析它们
查看allure_results
文件夹下的内容
查看了一下发现result.json
文件中包含了每个case执行的内容
{
"name": "主入口下方文案校验",
"status": "passed",
"description": "\n 按钮下方文案测试\n * 挂号\n * 问诊\n * 购药\n ",
"steps": [
{
"name": "查看首页_问诊按钮下方文案",
"status": "passed",
"steps": [
{
"name": "获取「首页_问诊下方文字」的文字",
"status": "passed",
"parameters": [
{
"name": "locator",
"value": "首页_问诊下方文字"
},
{
"name": "many",
"value": "False"
},
{
"name": "num",
"value": "0"
}
],
"start": 1647138093415,
"stop": 1647138093436
}
],
"start": 1647138093415,
"stop": 1647138093436
}
],
"parameters": [
{
"name": "way",
"value": "'inquiry'"
}
],
"start": 1647138093415,
"stop": 1647138093436,
"uuid": "d704fabe-a2cf-437b-ac6d-b814a0d33b7b",
"historyId": "1ca4e0e03325987a13faaed8432220f9",
"testCaseId": "ce83012d4892fd256d6496f512d294f3",
"fullName": "src.cases.test_home#test_home_text",
"labels": [
{
"name": "story",
"value": "首页"
},
{
"name": "feature",
"value": "微医主站"
},
{
"name": "parentSuite",
"value": "src.cases"
},
{
"name": "suite",
"value": "test_home"
},
{
"name": "host",
"value": "zhongxindeiMac.local"
},
{
"name": "thread",
"value": "84836-MainThread"
},
{
"name": "framework",
"value": "pytest"
},
{
"name": "language",
"value": "cpython3"
},
{
"name": "package",
"value": "src.cases.test_home"
}
]
}
所以第一步就是读取这些json文件,使用列表推导式拿到全部的result.json
文件
result_list = [i for i in os.listdir(path) if '-result.json' in i]
我们需要的数据大致为
用例名称(英文、中文、参数)
运行状态
运行耗时
错误信息
通过json模块来读取json并转化为字典类型
import json
with open(path, "r") as f:
json_data = json.load(f)
return json_data
full_name = json_data.get("fullName")
把labels
中关键内容合并一下
labels = '_'.join([i.get('value', '') for i in json_data.get("labels") if
i.get('name', '') in ['feature', 'story']]) + "_" + json_data.get("name")
通过参数化编写的case名称一样,参数不一样,所以需要拿到参数
parameters = json_data.get("parameters", '') and '_'.join([i.get('value', '') for i in
json_data.get("parameters", '')])
通过的用例不会有异常信息,所以判断一下再进行数据组合
statusDetails = json_data.get('statusDetails', '')
statusDetails1 = {
"message": "",
"trace": ""
}
if statusDetails:
try:
statusDetails1["message"] = statusDetails.get("message", "")
statusDetails1["trace"] = statusDetails.get("trace", "")
except Exception:
pass
data = {
"fullName": full_name,
"status": json_data.get("status"),
"labels": labels,
"duration": json_data.get("stop", 0) - json_data.get("start", 0),
"parameters": parameters,
"statusDetails": statusDetails1
}
判断一下存储的字典中有没有这个字段再进行存放
allure_results = []
allure_dict = {}
if f'{full_name}_{parameters}' not in allure_dict:
allure_dict[f'{full_name}_{parameters}'] = [data]
else:
allure_dict[f'{full_name}_{parameters}'].append(data)
使用了rerun
之后的用例会重复生成result.json
文件
需要判断有没有重跑成功,如果重跑都失败那就拿其中的失败结果,如果有成功的话就改为成功
for v in allure_dict.values():
if len(v) == 1:
allure_results.append(v[0])
else:
for v1 in v:
if 'pass' in v1.get("status"):
allure_results.append(v1)
break
else:
allure_results.append(v[0])
完整代码:
https://gitee.com/zx660644/uitest/blob/master/src/utils/jsonoperator.py