class AssertInfo:
def __init__(self, actual, expect):
self.data = []
self.compare(actual, expect)
def compare(self, response_data, assert_data):
# log.debug(f'{response_data}\n{assert_data}')
if assert_data == '*': # 通配符'*'
# log.debug(f'通配符不比较')
return
if isinstance(response_data, dict): # 字典
for key in assert_data:
if key not in response_data:
info = f'Response data has no key: {key}'
log.debug(info)
self.data.append(info)
for key in response_data:
if key in assert_data:
self.compare(response_data[key], assert_data[key])
else:
info = f'Assert data has no key: {key}'
log.debug(info)
self.data.append(info)
elif isinstance(assert_data, list): # 列表
if len(response_data) != len(assert_data):
info = f'长度不一致:{response_data}|||{assert_data}'
log.debug(info)
self.data.append(info)
return
if len(response_data) == 0:
log.debug(f'空列表不需要对比')
return
for i in range(len(response_data)):
self.compare(response_data[i], assert_data[i])
else: # 字符串 或 数字
if response_data != assert_data:
info = f'【{response_data}】不等于【{assert_data}】'
log.debug(info)
self.data.append(info)
if __name__ == '__main__':
from Config import MyLog
test = AssertInfo({"a": {"a1": '1', "b1": "b1", "c1": [{"c11": "c12"}, ["1", "2", 1, 2], 'C11']}, "b": {"b1": ""}},
{"a": {"a1": '*', "b1": "b11", "c1": [{"c11": "c123"}, ["1", "22", 1, 22], 'C11']}, "b": '*'})
# test = AssertInfo({"a": {"a1": '1', "b1": "b1", "c1": [{"c11": "c12"}, ["1", "2", 1, 2], 'C11']}, "b": {"b1": ""}},
# {"a": {"a1": '*', "b1": "b1", "c1": [{"c11": "c12"}, ["1", "2", 1, 2], 'C11']}, "b": '*'})
log.debug(test.data)