注意此文章中的函数需调用另外一篇文章中的py文件中的函数,请点击python3+接口自动化测试+接口依赖处理+定义全局变量global_value.py文件,添加set和get函数接口依赖处理-utils目录:handle_depend.py 流程图-01: 函数write_g_val(参数1,参数2): 没有返回值,将值写入全局变量 流程图-02: 函数replace_params(参数1,参数2): 有返回值,返回替换参数后的case_parameter 代码实现:
import json
from conf.global_value import GlobalValue
global_value = GlobalValue()
class HandleDepend:
def write_g_val(self, response_content_dict, is_getvalue_dict):
"""调用获取响应字段函数,然后将值写入变量文件中"""
# write_values = self.get_value_by_resp(response_content_dict, is_getvalue_dict)
"""将获取的值存放全局变量"""
# global_value.set_value_by_key(write_values)
new_value = is_getvalue_dict
for k, v in is_getvalue_dict.items():
if '$' in v:
var_str = v.lstrip('$')
var_list = var_str.split('.') # ['result', 'list[0]', 'id']
value = response_content_dict
for var in var_list:
if '[' in var:
list_name = var.split('[')[0] # list
list_num = int(var.split('[')[1].strip(']')) # n
value = value[list_name][list_num]
elif len(var_list) > 1:
value = value[var]
# print(value)
new_value[k] = value
global_value.set_value_by_key(new_value)
def replace_params(self, case_parameter_dict, is_depend_dict):
"""
根据case['is_depend']替换case['parameter']返回替换参数后的case['parameter']
:param case_parameter_dict:接口参数,字典类型
:param is_depend_dict:excel测试用例表中的case['is_depend']值,字典类型
:return:返回替换参数后的case_parameter,字典类型
"""
new_case_parameter = case_parameter_dict
for k, v in is_depend_dict.items():
if '$' in v:
"""调用global_value函数,根据v字段获取对应值"""
get_g_val = global_value.get_value_by_key(v.lstrip('$'))
# print(get_g_val) # 34
else:
get_g_val = v
if '.' in k:
"""最多处理2级,即支持最多有1个点的参数,共有4种场景"""
list_k = k.split('.') # list[0].id
if len(list_k) == 2:
if '[' in list_k[0]:
name1 = list_k[0].split('[')[0]
num1 = int(list_k[0].split('[')[1].strip(']'))
if '[' in list_k[1]:
name2 = list_k[1].split('[')[0]
num2 = int(list_k[1].split('[')[1].strip(']'))
new_case_parameter[name1][num1][name2][num2] = get_g_val
else:
name2 = list_k[1]
new_case_parameter[name1][num1][name2] = get_g_val
else:
name1 = list_k[0]
if '[' in list_k[1]:
name2 = list_k[1].split('[')[0]
num2 = int(list_k[1].split('[')[1].strip(']'))
new_case_parameter[name1][name2][num2] = get_g_val
else:
name2 = list_k[1]
new_case_parameter[name1][name2] = get_g_val
else:
"""处理单级的参数,如: list , list[0]"""
if '[' in k:
name = k.split('[')[0] # userIds
num = int(k.split('[')[1].strip(']')) # 0或1
# new_item = '{0}[{1}]'.format(name, num) # list[0]或list[1]
# print(new_item)
new_case_parameter[name][num] = get_g_val
else:
new_case_parameter[k] = get_g_val
return new_case_parameter
if __name__ == '__main__':
handle_depend = HandleDepend()
"""接口响应内容如下"""
res_dict={
"message":"test",
"result":{
"conid":"123",
"id":"234"
},
"success":"OK"
}
"""需要获取的响应字段值"""
is_getvalue = {
"con_id": "$result.conid"
}
"""调用函数,将获取的响应字段值放入全局变量,用于后续接口的参数替换"""
handle_depend.write_g_val(res_dict, is_getvalue)
"""接口参数如下"""
case_parameter={
"list":
[
"id":"123456",
"test":"654321"
]
}
"""需要替换的参数"""
is_depend = {"list[0].id": "$con_id"}
"""调用函数,替换接口的参数"""
handle_depend.replace_params(case_parameter, is_depend)
print(case_parameter['list'][0]['id'])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)