Advertisement
Guest User

Pytest deepdiff plugin

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. def pytest_assertrepr_compare(config, op, left, right):
  2.     """Show objects' diffs in assert-equal fails using deepdiff"""
  3.     from pprint import pformat
  4.     from deepdiff import DeepDiff
  5.     from mock import call
  6.    
  7.     config.option.verbose = 2  # disable output truncation, deepdiff output can be large
  8.  
  9.     call_type = type(call)
  10.  
  11.     def isiterable(obj):
  12.         try:
  13.             iter(obj)
  14.             return not isinstance(obj, basestring)
  15.         except TypeError:
  16.             return False
  17.  
  18.     def is_call(obj):
  19.         if isinstance(obj, call_type) or any(isinstance(item, call_type) for item in obj):
  20.             return True
  21.  
  22.     def call_to_tuple(obj):
  23.         if isinstance(obj, call_type):
  24.             return tuple(obj)
  25.         else:
  26.             return map(tuple, obj)
  27.  
  28.     explanation = None
  29.     try:
  30.         if op == '==':
  31.             if isiterable(left) and isiterable(right):
  32.                 if is_call(left) or is_call(right):
  33.                     explanation = DeepDiff(call_to_tuple(left), call_to_tuple(right))
  34.                 else:
  35.                     explanation = DeepDiff(left, right)
  36.  
  37.                 explanation = pformat(explanation, indent=2).split('\n')
  38.     except Exception as e:
  39.         explanation = [
  40.             '(deepdiff plugin: representation of details failed.  ',
  41.             'Probably an object has a faulty __repr__.)',
  42.             str(e)
  43.         ]
  44.  
  45.     return explanation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement