前三篇文章简要说明了接口diff的来龙去脉,如下
今天我们介绍一下接口diff的核心diff模块代码实现原理和「进阶玩法」
def contrast_swagger(result, expected):
"""
:param result:
:param expected:
:return:
"""
# ignore_order 忽略重复的 列出不同的地方
# ignore_string_case 忽略大小写
cmp_dict = DeepDiff(result, expected, ignore_order=True).to_dict()
if expected is None:
log.error("传入的json为空,请检查!")
return None, False
else:
if cmp_dict.get("dictionary_item_added") or cmp_dict.get("dictionary_item_removed") or cmp_dict.get(
"values_changed"):
log.info(cmp_dict)
log.info("接口结构有变换!")
return cmp_dict, False
else:
return cmp_dict, True
def merge_swagger(add_str, new_json):
"""
合并更新生成新的json
:param add_str:
:param new_json:
:return:
"""
global key
add_str = add_str.replace('root', '')
add_str = add_str.replace('[', '')
add_str = add_str.replace("'", '')
keys = add_str.split(']')[:-1]
data = {}
for key in reversed(keys):
if not data:
data[key] = None
else:
data = {key: data}
value = None
v_ref = data
for item, key in enumerate(keys, 1):
if not value:
value = new_json.get(key)
else:
value = value.get(key)
if item < len(keys):
v_ref = v_ref.get(key)
v_ref[key] = value
return data
def format_swagger_v1(diff_result, latest_swagger):
"""
:param latest_swagger: 最新的 swagger json
:param diff_result:对比结果
:return:格式化合并完成的 swagger json
"""
added_dict = []
try:
add_result = diff_result['dictionary_item_added']
except KeyError:
log.error('diff结果中无新增项!')
return None
for added_str in add_result:
added_dict.append(merge_swagger(add_str=added_str, new_json=latest_swagger))
new_list = []
for i in added_dict:
try:
new_list.append(str(i['paths']).lstrip('{').replace('}}}}}', '}}}}'))
except KeyError:
pass
latest_swagger['paths'] = {str(new_list).replace('["', '').replace('"]', '').replace('"', '')}
merge_swagger_json = json.loads(
str(latest_swagger).replace('"', '').replace("'", '"').replace('False', 'false').replace('True', 'true'))
return merge_swagger_json
def format_swagger_v2(diff_result, latest_swagger):
"""
:param latest_swagger: 最新的 swagger json
:param diff_result:对比结果
:return:格式化合并完成的 swagger json
"""
added_dict = []
try:
add_result = diff_result['dictionary_item_added']
except KeyError:
log.error('diff结果中无新增项!')
return None
for added_str in add_result:
added_dict.append(merge_swagger(add_str=added_str, new_json=latest_swagger))
new_list = []
for i in added_dict:
try:
new_list.append(str(i['paths']).lstrip('{').replace('}}}}}', '}}}}'))
except KeyError:
pass
latest_swagger['paths'] = {
str(new_list).replace('["', '').replace('"]', '').replace('"', '').replace('}}},', '}},').replace('}}}}',
'}}}').replace(
'False', 'false').replace('True', 'true')}
merge_swagger_json = json.loads(str(latest_swagger).replace('{"', '{').replace('}}}"}', '}}}').replace("'", '"'))
return merge_swagger_json
pip install -U swaggerjmx-diff
# -*- coding: utf-8 -*-
from swaggerjmx_diff.diff import *
with open('open-api-v1.json', 'r', encoding='utf8')as fp:
json_data_v1 = json.load(fp)
with open('open-api-v2.json', 'r', encoding='utf8')as fp:
json_data_v2 = json.load(fp)
contrast_result, result = contrast_swagger(json_data_v1, json_data_v2)
format_swagger_v2(diff_result=contrast_result, latest_swagger=json_data_v2)
pip install -U swaggerjmx
pip install -U swaggerjmx-diff
「将接口监控&接口diff&接口生成工具与CICD相结合」
开发提测->触发接口监控->触发接口diff->触发接口生成工具->生成新增接口
将我们的各种测试工具「模块化」,并进行自由搭配,可能会产生新的灵感哦!
关注「测试开发囤货」公众号回复「AI」,送你一套 Python机器学习 电子书。