A very efficient json comparison library in Python --deepdiff

In our work, we often need to distinguish between two pieces of code, or need to check whether the fields returned by the interface are consistent with expectations. How to quickly locate the difference between the two? In addition to some comparison tools such as Beyond Compare , WinMerge , etc., or the command tool diff (used in the linux environment), Python also provides many libraries for comparison, such as deepdiff and difflib. The difference between the two is displayed by deepdiff The comparison effect is relatively simple, but you can set ignored fields, and the comparison results displayed by difflib can be html, which is more detailed. Today we will learn about a library that quickly implements code and file comparisons - deepdiff .

what is deepdiff

deepdiffThe module is often used to verify whether two objects are consistent, including 3 commonly used classes, DeepDiff, DeepSearch and DeepHash, among which DeepDiff is the most commonly used, which can compare dictionaries, iterable objects, strings, etc., and find all differences recursively. Of course, it can also be used to verify the differences in the content of various files, such as txt, json, pictures, etc...

https://github.com/seperman/deepdiff

deepdiff install

pip install deepdiff

If the actual request result is consistent with the json data of the expected value, then {} empty dictionary will be returned; otherwise, the result of comparing the difference will be returned. In the interface test, we can also make assertions based on this feature.
import

>>> from deepdiff import DeepDiff  # For Deep Difference of 2 objects
>>> from deepdiff import grep, DeepSearch  # For finding if item exists in an object
>>> from deepdiff import DeepHash  # For hashing objects based on their contents

If the comparison results are different, the following corresponding returns will be given:

  • 1. type_changes: key of type change
  • 2. values_changed: the key whose value has changed
  • 3. dictionary_item_added: dictionary key added
  • 4. dictionary_item_removed: field key deleted

Case 1. Compare txt files

from deepdiff import DeepDiff
"""
a.txt的内容是: abc
b.txt的内容是: abcd
"""
f1, f2 = open('a.txt', 'r', encoding='utf-8').read(), open('b.txt', 'r', encoding='utf-8').read()
print(DeepDiff(f1, f2))  
# 输出结果,内容值发生变化 {'values_changed': {'root': {'new_value': 'abcd', 'old_value': 'abc'}}}

Case 2, compare json


from deepdiff import  DeepDiff
json1={
    'code': 0,
    "message": "成功",
    "data": {
        "total": 28,
        "id":123
}
}
json2={
    'code':0,
    "message":"成功",
    "data": {
        "total": 29,
    }
}
print(DeepDiff(json1,json2))
# 输出结果,id移除,total值发生改变
#{'dictionary_item_removed': [root['data']['id']], 'values_changed': {"root['data']['total']": {'new_value': 29, 'old_value': 28}}}

Application of DeepDiff in unit testing

import unittest
import requests
from deepdiff import DeepDiff
class MyCase(unittest.TestCase):
    expect = {
        'slideshow': {
            'author': 'Yours Truly',
            'date': 'date of publication',
            'slides': [{
                'title': 'Wake up to WonderWidgets!',
                'type': 'all'
            }, {
                'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'],
                'title': 'Overview',
                'type': 'all'
            }],
            'title': 'Sample Slide Show'
        }
    }
    def setUp(self):
        self.response = requests.get('http://www.httpbin.org/json').json()
        print(self.response)
    def test_case_01(self):
        print(DeepDiff(self.response, self.expect))
    def test_case_02(self):
        print(DeepDiff(self.response['slideshow']['author'], 'Yours Truly1'))
if __name__ == '__main__':
    unittest.main()

image-20220715172541928

The actual return of test case 1 is exactly the same as the expected result json, and the output result is: {}, that is, there is no difference between the two.

Test case 2 asserts that author returns the expected value, and the value changes.

In fact, in the actual interface assertion, the order of the fields that need to be verified may be different, or some field values ​​​​are not needed. In order to solve this kind of problem, Deepdiff also provides reliable parameters, which only need to be added when comparing. Enter the corresponding parameters.

  • ignore order(ignore sorting)
  • ignore string case(ignore case)
  • exclude_pathsExclude specified fields
print(DeepDiff(self.response, self.expect,view='tree',ignore_order=True,ignore_string_case=True,exclude_paths={"root['slideshow']['date']"}))

More parameters can be used, enter the source code to view:

image-20220715183110925

For more information on the use of DeepDiff, you can view the following documents:

  • https://zepworks.com/deepdiff/5.8.2/diff.html
  • https://zepworks.com/deepdiff/5.8.2/
  • https://zepworks.com/tags/deepdiff/

For more software testing information, please visit my personal website: https://www.iltesting.com/

Guess you like

Origin blog.csdn.net/XingLongSKY/article/details/125820451