Python对比校验神器deepdiff库使用详解
作者:爱学习de测试小白 发布时间:2023-05-14 11:05:35
工作中我们经常要两段代码的区别,或者需要查看接口返回的字段与预期是否一致,如何快速定位出两者的差异?除了一些对比的工具比如Beyond Compare、WinMerge等,或者命令工具diff(在linux环境下使用),其实Python中也提供了很多实现对比的库,比如deepdiff和difflib,这两个的区别是deepdiff显示的对比效果比较简洁,但是可以设置忽略的字段,difflib显示的对比结果可以是html的,比较详细。今天我们就学习一下快速实现代码和文件对比的库–deepdiff。
前言
在接口自动化中会遇到想要得出两次响应体(json值)差异,本篇来学习的deepdiff库可以解决这问题
deepdiff库
安装
pip install deepdiff
说明
deepdiff模块常用来校验两个对象是否一致,并找出其中差异之处,它提供了:
deepdiff模块常用来校验两个对象是否一致,并找出其中差异之处,它提供了:
DeepDiff:比较两个对象,对象可以是字段、字符串等可迭代的对象
DeepSearch:在对象中搜索其他对象
DeepHash:根据对象的内容进行哈希处理
DeepDiff
作用:比较两个对象,对象可以是字段、字符串等可迭代的对象
说明:
type_changes:类型改变的key
values_changed:值发生变化的key
dictionary_item_added:字典key添加
dictionary_item_removed:字段key删除
对比json
# -*-coding:utf-8一*-
# @Time:2023/4/16
# @Author: DH
from deepdiff import DeepDiff
# json校验
json_one = {
'code': 0,
"message": "失败",
'data': {
'id': 1
}
}
json_two = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
print(DeepDiff(json_one, json_two))
# 输出
"""
{'values_changed': {"root['code']": {'new_value': 1, 'old_value': 0}, "root['message']": {'new_value': '成功', 'old_value': '失败'}}}
root['code'] : 改变值的路径
new_value : 新值
old_value :原值
"""
列表校验
cutoff_distance_for_pairs: (1 >= float > 0,默认值=0.3);通常结合ignore_order=true使用,用于结果中展示差异的深度。值越高,则结果中展示的差异深度越高。
from deepdiff import DeepDiff
t1 = [[[1.0, 666], 888]]
t2 = [[[20.0, 666], 999]]
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.5))
print(DeepDiff(t1, t2, ignore_order=True)) # 默认为0.3
print(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.2))
"""
{'values_changed': {'root[0][0]': {'new_value': [20.0, 666], 'old_value': [1.0, 666]}, 'root[0][1]': {'new_value': 999, 'old_value': 888}}}
{'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
{'values_changed': {'root[0]': {'new_value': [[20.0, 666], 999], 'old_value': [[1.0, 666], 888]}}}
"""
忽略字符串类型
ignore_string_type_changes :忽略校验字符串类型,默认为False
print(DeepDiff(b'hello', 'hello', ignore_string_type_changes=True))
print(DeepDiff(b'hello', 'hello'))
"""
输出:
{}
{'type_changes': {'root': {'old_type': <class 'bytes'>, 'new_type': <class 'str'>, 'old_value': b'hello', 'new_value': 'hello'}}}
"""
忽略大小写
ignore_string_case:忽略大小写,默认为False
from deepdiff import DeepDiff
print(DeepDiff(t1='Hello', t2='heLLO'))
print(DeepDiff(t1='Hello', t2='heLLO', ignore_string_case=True))
"""
输出:
{'values_changed': {'root': {'new_value': 'heLLO', 'old_value': 'Hello'}}}
{}
"""
DeepSearch
作用:在对象中搜索其他对象 查找字典key/value
from deepdiff import DeepSearch
json_three = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
# 查找key
print(DeepSearch(json_three, "code"))
print(DeepSearch(json_three, "name"))
# 查找value
print(DeepSearch(json_three, 1))
"""
输出:
{'matched_paths': ["root['code']"]}
{}
{'matched_values': ["root['code']", "root['data']['id']"]}
"""
# 正则 use_regexp
obj = ["long somewhere", "string", 0, "somewhere great!"]
# 使用正则表达式
item = "some*"
ds = DeepSearch(obj, item, use_regexp=True)
print(ds)
# 强校验 strict_checking 默认True
item = '0'
ds = DeepSearch(obj, item, strict_checking=False)
# ds = DeepSearch(obj, item) # 默认True
print(ds)
# 大小写敏感 case_sensitive 默认 False 敏感
item = 'someWhere'
ds = DeepSearch(obj, item, case_sensitive=True)
print(ds)
DeepHash
作用:根据对象的内容进行哈希处理
from deepdiff import DeepHash
# 对对象进行hash
json_four = {
'code': 1,
"message": "成功",
'data': {
'id': 1
}
}
print(DeepHash(json_four))
extract
extract : 根据路径查询值
from deepdiff import extract
# 根据路径查询值
obj = {1: [{'2': 666}, 3], 2: [4, 5]}
path = "root[1][0]['2']"
value = extract(obj, path)
print(value)
"""
输出:
666
"""
grep
搜索
from deepdiff import grep
obj = ["long somewhere", "string", 0, "somewhere great!"]
item = "somewhere"
ds = obj | grep(item)
print(ds)
# use_regexp 为True 表示支持正则
obj = ["something here", {"long": "somewhere", "someone": 2, 0: 0, "somewhere": "around"}]
ds = obj | grep("some.*", use_regexp=True)
print(ds)
# 根据值查询路径
obj = {1: [{'2': 'b'}, 3], 2: [4, 5, 5]}
result = obj | grep(5)
print(result)
"""
输出:
{'matched_values': ['root[2][1]', 'root[2][2]']}
"""
来源:https://blog.csdn.net/IT_heima/article/details/130148745


猜你喜欢
- 下面直接上代码留存,方便以后查阅复用。# -*- coding: utf-8 -*- #作者:LeniyTsan#时间:2014-07-17
- 本文实例讲述了jQuery实现的简单分页。分享给大家供大家参考,具体如下:<!DOCTYPE html PUBLIC "-/
- 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1
- 在开始使用Go进行编码时,Defer是要关注的一个很重要的特性。它非常简单:在任何函数中,给其他函数的调用加上前缀 defer以确保该函数在
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下#!/usr/bin/env python# -*- c
- 一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:#python >>>f=open('f
- 问题你想对在Unix系统上面运行的程序设置内存或CPU的使用限制。解决方案resource 模块能同时执行这两个任务。例如,要限制CPU时间
- 通常,在完成了一件网页设计后,设计师的无知都会显露无遗而备受指责。他们把创建网页代码的繁重工作都留给了程序员们。这种现象不只出现在网络开发行
- python版本:3.8class object: """ The most base type "
- 前言业务需求中需要连接两个数据库处理数据,需要用动态数据源。通过了解mybatis的框架,计划 使用分包的方式进行数据源的区分。原理前提:我
- 日常项目中,读取各种配置文件是避免不了的,这里介绍一个能读取多种配置文件的库,viperviper读取ini文件config := vipe
- 楼主在做公司项目的时候遇到url重定向的问题,因此上网简单查找,作出如下结果由于使用的是语言是python所以以下是python的简单解决方
- 现有两个元组(('a'),('b')),(('c'),('d')),请使用p
- MySQL的sql_mode合理设置sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些
- 本文实例为大家分享了python实现UDP文件传输的具体代码,供大家参考,具体内容如下UDP协议下文件传输:服务端import socket
- Django是一个大而全的框架。需要明确的是,传参进行分页获取分页后的数据,一般都是通过向服务器发送get请求的方式实现的,在向后端服务发送
- 楔子Python 有一个第三方模块叫 psutil,专门用来获取操作系统以及硬件相关的信息,比如:CPU、磁盘、网络、内存等等。下面来看一下
- MySQL日志文件相信大家都有很多的了解,MySQL日志文件一般在:/var/log/mysqld.log,下面就教您修改MySQL日志文件
- vendorvendor概念最早是由Keith提出,用来存放依赖包。在版本1.5出现。例如gb项目提供了一个名为gsftp的示例项目,它有一
- 前提:安装xhtml2pdf https://pypi.python.org/pypi/xhtml2pdf/下载字体:微软雅黑;待转换的文件