在功能测试的情况下,有太多的用例数据需要被创造出来,我们可以用接口来快速的打造数据,从而进行高效率的解决问题
{
"body": {
"Version": "317",
"config": [
{
"Id": "370",
"TypeId": "8"
},
{
"Id": "371",
"TypeId": "8"
}
],
"Name": "数据池",
"config": [
{
"Code": "150",
"number": "177"
}
]
},
"id": "1234",
}
- {
"body": {
"Version": "43253",
"Name": "名字在这里"
}
}
- {
"body": {
"Version": "98765",
"Name": "名称"
}
}
"""
@Time :
@Author :
@Email :
@File : yamlutil.py
@Project :
@feature : 实现自动填充 yaml 模板格式数据(针对三层json数据的转换)
@实现步骤:
1. 获取 yaml 文件
2. 遍历文件内容,用字典的形式进行内容替换
"""
import os
import copy
import yaml
from common.base.get_config import cf # 路径的自创类
class YamlUtil:
@staticmethod
def load_data(model, data):
"""
:param data_path: 残缺用例数据
:param data_model_path: 请求体模板
:return: 按请求体模板,返回完整的用例数据
"""
data_test = yaml.safe_load(open(data, encoding="utf-8"))
data_model = yaml.safe_load(open(model, encoding="utf-8"))
# 遍历读取 test 用例残缺数据
for i in range(len(data_test)):
# 进行深度拷贝,用例模板,防止变量地址相同
data_model_copy = copy.deepcopy(data_model)
# 取出 test 用例残缺数据
for key, value in data_test[i].items():
# 判断字典值是否为 字典
if type(value) is dict:
# 为字典,进入遍历子字典改值
for k, v in value.items():
# 判断字典值是否为 字典
if type(v) is dict:
# 为字典,进入遍历子字典改值
for k1, v1 in v.items():
data_model_copy[key][k][k1] = v1
# 判断字典值是否为 列表
elif type(v) is list:
# 为列表,进入遍历列表
for j in range(len(v)):
for k2, v2 in data_test[j].items():
data_model_copy[key][k][k2] = v2
else:
data_model_copy[key][k] = v
else:
# 不为字典,不进入,直接改值
data_model_copy[key] = value
# 替换完字段以后,把生成值替换列表内容
data_test[i] = data_model_copy
return data_test
yamlutil = YamlUtil()
print(yamlutil.load_data(cf.DATA_MODEL, cf.TEST_DATA))