python 接口diff工具:deepdiff

一般用做接口版本升级后前后返回的数据格式是否有误,或者两个环境的接口是否是一致
1、需要安装diff库:pip install deepdiff
2、最基本的两个json比较

from deepdiff import DeepDiff
from pprint import pprint
# t1 = {"one": 1, "two": 2, "three": 3}
# t2 = {"one": 1, "two": 4, "three": "6"}
if __name__ == "__main__":
    res = DeepDiff(t1, t2,ignore_order)
    pprint(res)

输出结果:
python 接口diff工具:deepdiff_第1张图片
image.png

通过diff,two的值不一样,three的类型不一样
pprint是输出格式更完整,标准化
3、忽略顺序或重复项列出差异

from deepdiff import DeepDiff
from pprint import pprint
t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
if __name__ == "__main__":
    res = DeepDiff(t1, t2,ignore_order=True)
    # res = DeepDiff(t1, t2)
    pprint(res)

运行结果:
image.png

即两者没有差异,因为忽略了b列表的顺序及重复项
4、从比较中排除对象树的一部分,如token、uuid等容易变化的数

from deepdiff import DeepDiff
from pprint import pprint
t1 = {"for life": "vegan", "ingredients": ["no meat", "no eggs", "no dairy"]}
t2 = {"for life": "vegan", "ingredients": ["veggies", "tofu", "soy sauce"]}
if __name__ == "__main__":
    res = DeepDiff(t1, t2,exclude_paths={"root['ingredients']"})
    pprint(res)

运行结果:{},表示忽略了ingredients的对比

https://github.com/seperman/deepdiff
https://pypi.org/project/deepdiff/

你可能感兴趣的:(python 接口diff工具:deepdiff)