在接口自动化测试过程中,我们会将接口返回的响应值与我们的预期值进行断言,从而来判断该接口测试是PASS还是FAIL,当代码写完运行时发现写入到excle中的两个值格式完全一样,但是一个为双引号,一个为单引号,导致在断言的时候断言为失败。
中间想过很多办法,都由于一些原因尝试失败,比如说替换,最后找了一个一劳永逸的万能法,就是分别将两个响应值进行处理,将每一个value值都拿出来,放进一个列表中,最后对这两个列表进行比较,完美解决问题
list1 = []
def _dict(singer_dict):
global list1
keys = singer_dict.keys()
for key in keys:
value = singer_dict[key]
if type(value).__name__ == 'list':
_list(value)
elif type(value).__name__ == 'dict':
_dict(value)
elif type(value) == int or type(value) == str or type(value) == bool:
list1.append(value)
return list1
def _list(list_data):
global list1
for singer_list in list_data:
if type(singer_list).__name__ == 'dict':
_dict(singer_list)
elif type(singer_list).__name__ == 'list':
_list(singer_list)
elif type(singer_list) == int or type(singer_list) == str or type(singer_list) == bool:
list1.append(singer_list)
return list1
def run_for(dict1):
global list1
list1 = []
keys1 = dict1.keys()
for key in keys1:
value1 = dict1[key]
if type(value1).__name__ == 'list':
_list(value1)
elif type(value1).__name__ == 'dict':
_dict(value1)
elif type(value1) == int or type(value1) == str or type(value1) == bool:
list1.append(value1)
return list1
dict2 = {'key5':'v4','key6':{'key7':'v5','key8':'v6'}}
dict1 = {'key1':[{'key2':'v1'},{'key3':'v2'},{'key4':'v3'}],'key5':'v4','key6':{'key7':'v5','key8':'v6'}}
print(run_for(dict1))
print(run_for(dict2))
?执行结果:
这里有一个坑就是在对数据的类型进行判断的时候,需要用到'__name__'。?
|