增加json文件转yaml文件,修改发送企业微信群聊报错用例字体颜色
This commit is contained in:
parent
0c58e4338d
commit
0a6ffdbcfb
|
@ -69,13 +69,13 @@ class EnterpriseWechatNotification:
|
|||
> 测试环境:{ENV.name}
|
||||
> 总用例数:<font color=\"info\">{self.pytest_result['total_case']}条</font>
|
||||
> 通过用例数:<font color=\"info\">{self.pytest_result['pass_case']}条</font>
|
||||
> 失败用例数:<font color=\"warning\">{self.pytest_result['fail_case']}条</font>
|
||||
> 报错用例数:<font color=\"warning\">{self.pytest_result['error_case']}条</font>
|
||||
> 跳过用例数:<font color=\"comment\">{self.pytest_result['skip_case']}条</font>
|
||||
> 失败用例数:<font color=\"red\">{self.pytest_result['fail_case']}条</font>
|
||||
> 报错用例数:<font color=\"red\">{self.pytest_result['error_case']}条</font>
|
||||
> 跳过用例数:<font color=\"warning\">{self.pytest_result['skip_case']}条</font>
|
||||
> 预期失败用例数:<font color=\"comment\">{self.pytest_result['xfail_case']}条</font>
|
||||
> 预期通过用例数:<font color=\"comment\">{self.pytest_result['xpass_case']}条</font>
|
||||
> 通过率:<font color=\"info\">{self.pytest_result['pass_rate']}%</font>
|
||||
> 用例执行时间:<font color=\"info\">{self.pytest_result['case_duration']}s</font>
|
||||
> 用例执行时长:<font color=\"info\">{self.pytest_result['case_duration']}s</font>
|
||||
> 测试报告,点击查看>>[测试报告入口]({ALLURE_URL})
|
||||
> 构建详情,点击查看>>[控制台入口]({BUILD_URL})
|
||||
> {content}"""
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding:utf-8 -*-
|
||||
import json
|
||||
import os
|
||||
import yaml
|
||||
|
||||
from common.exceptions import ValueTypeError
|
||||
from configs.dir_path_config import BASE_DIR
|
||||
|
||||
|
||||
|
@ -11,7 +13,7 @@ class YamlHandle:
|
|||
|
||||
def __init__(self, dir_file_name):
|
||||
"""
|
||||
:param dir_file_name: 项目下文件所在目录名及文件名,如:datas/login.yaml、datas/automatic_datas/login.yaml
|
||||
:param dir_file_name: 项目下文件所在目录名及文件名,如:datas/login.yaml、datas/automatic_datas/login.yaml,也可以传绝对路径
|
||||
"""
|
||||
file_path = os.path.join(BASE_DIR, dir_file_name)
|
||||
self.file_path = file_path
|
||||
|
@ -34,9 +36,55 @@ class YamlHandle:
|
|||
with open(self.file_path, mode=mode, encoding="utf-8") as f:
|
||||
yaml.dump(data, f)
|
||||
|
||||
@staticmethod
|
||||
def json_to_yaml(json_or_dict_data):
|
||||
"""
|
||||
将json或dict格式的数据转换成yaml格式的数据
|
||||
:param json_or_dict_data: 传入json字符串或字典格式数据
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
if isinstance(json_or_dict_data, dict):
|
||||
# 判断是dict格式,直接将Python字典转换为YAML格式字符串
|
||||
yaml_data = yaml.dump(json_or_dict_data, default_flow_style=False)
|
||||
else:
|
||||
# 否则解析JSON字符串为Python字典
|
||||
json_data = json.loads(json_or_dict_data)
|
||||
# 将Python字典转换为YAML格式字符串
|
||||
yaml_data = yaml.dump(json_data, default_flow_style=False)
|
||||
|
||||
return yaml_data
|
||||
except Exception as e:
|
||||
raise ValueTypeError(f"转换失败,请确认传入的数据是否是json格式或字典格式,错误信息: {str(e)}")
|
||||
|
||||
def json_to_yaml_file(self, yaml_file_path):
|
||||
"""
|
||||
将json文件转换成yaml文件
|
||||
:param yaml_file_path: 需要保存的yaml文件相对路径或绝对路径,相对路径如:datas/login.yaml、datas/automatic_datas/login.yaml
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
# 读取JSON文件内容
|
||||
with open(self.file_path, 'r') as json_file:
|
||||
json_data = json.load(json_file)
|
||||
|
||||
# 打开YAML文件并将YAML格式数据写入
|
||||
yaml_file_path = os.path.join(BASE_DIR, yaml_file_path)
|
||||
with open(yaml_file_path, 'w') as yaml_file:
|
||||
yaml.dump(json_data, yaml_file, default_flow_style=False)
|
||||
except Exception as e:
|
||||
raise ValueTypeError(f"转换失败,请确认传入的文件是否是json数据文件或文件内数据是否是json格式,错误信息:{str(e)}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(YamlHandle('datas/login.yaml').read_yaml())
|
||||
data_smsCode = YamlHandle('datas/login.yaml').read_yaml()
|
||||
params = [(item['phone'], item['smsCode'], item['expected']) for item in data_smsCode]
|
||||
print(params)
|
||||
# print(YamlHandle('datas/login.yaml').read_yaml())
|
||||
# data_smsCode = YamlHandle('datas/login.yaml').read_yaml()
|
||||
# params = [(item['phone'], item['smsCode'], item['expected']) for item in data_smsCode]
|
||||
# print(params)
|
||||
|
||||
# 示例JSON数据
|
||||
json_string = '{"name": "John", "age": 30, "city": "New York"}'
|
||||
|
||||
# 调用方法进行转换
|
||||
yaml_result = YamlHandle.json_to_yaml(json_string)
|
||||
print(yaml_result)
|
||||
|
|
Loading…
Reference in New Issue