测试用例的 yaml 数据
-
name: 测试用例1-缓存管理-FDNFD运价缓存管理-正常添加
severity: "BLOCKER"
is_database: 1
request:
method: post
url: AddFDNFDFreightRateCacheMag
headers: null
json: { "ct": "BasicData","cv": "1.0.1","tk": $..token,"v": { "AirLineCode": "8L","CacheVailidityUnit": 1,
"CacheValidity": 20,"FreightRateSource": 1,"FreightRateType": 1,"Status": 1 } }
validate:
expect_code: 200
expect_msg: "OK"
sql_code: "select AirLineCode from fd_nfd_freight_rate_cache_mag WHERE AirLineCode='8L'"
替换数据里参数的方法
def replace_value(self, data):
if data:
# 保存数据类型
data_type = type(data)
# 判断数据类型转换成str
if isinstance(data, dict) or isinstance(data, list):
#将python数据结构转换为json
str_data = json.dumps(data)
else:
str_data = str(data)
for cs in range(1, str_data.count('${') + 1):
# 替换
#判断 { } 括号是否包含在数据中
if "${" in str_data and "}" in str_data:
#获取{ 的索引
start_index = str_data.index("${")
#获取} 的索引
end_index = str_data.index("}", start_index)
#数据截取
old_value = str_data[start_index:end_index + 1]
#
new_value = read_yaml(old_value[2:-1])
#新数据替换旧的数据
str_data = str_data.replace(old_value, new_value)
# 还原数据类型
if isinstance(data, dict) or isinstance(data, list):
#json字符串解码为python对象
data = json.loads(str_data)
else:
#强制转为开始定义时的数据类型
data = data_type(str_data)
return data
封装发送请求的方法
sess = requests.session()
# 统一请求封装
def send_request(self, method, url, **kwargs):
method = str(method).lower() # 转换小写
# 基础路径的拼接和替换
url = self.base_url + self.replace_value(url)
print(url)
# 参数替换
for key, value in kwargs.items():
if key in ['params', 'data', 'json', 'headers']:
kwargs[key] = self.replace_value(value)
elif key == "files":
for file_key, file_path in value.items():
value[file_key] = open(file_path, 'rb')
res = RequestUtil.sess.request(method, url, **kwargs)
print(res.text)
return res