There are 2 dict:

STANDARD_DATA = { 'applications': [{ 'application': 'CUSTOMER', 'platforms': [ {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'ANDROID'}, {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'IOS'} ] }, { 'application': 'OWNER', 'platforms': [ {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'ANDROID'}, {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'IOS'} ] }] } UPDATED_DATA = { 'applications': [{ 'application': 'OWNER', 'platforms': [ {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'ANDROID'}, {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'IOS'} ] }, { 'application': 'CUSTOMER', 'platforms': [ {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'IOS'}, {'forceUpdate': [], 'latest': '1.19', 'minimal': '1.17', 'platform': 'ANDROID'} ] }] } 

Those. they are very similar, but the data inside is arranged in different orders: OWNER and CUSTOMER are on the contrary and inside CUSTOMER'a ANDROID and IOS swapped places.

Question:

How to write assert where these dict will be equal?

Language: Python 3.5

  • After formatting, I noticed that there is still minimal difference between the values, so in this case there is no way) - andreymal

3 answers 3

For tests, you can use TestCase.assertCountEqual() from the standard library to compare lists without order.

This does not help for dict lists. To implement a case-less comparison for an arbitrary list and to get a readable diff when calling assertDictEqual() , you can define your own class, which organizes repr() for the list:

 class unordered(list): def __repr__(self): # for showing the diff return "[%s]" % ", ".join(sorted(map(repr, self))) def __eq__(self, other): # for comparison return repr(self) == repr(other) 

To replace possibly deeply nested lists in the dictionary, you can use the recursive function

 def make_list_unordered(nested): for key, value in getattr(nested, 'items', lambda: enumerate(nested))(): if isinstance(value, (list, dict)): if isinstance(value, list): nested[key] = unordered(value) make_list_unordered(value) # transform nested items 

If desired, you can replace a wider class by using the MutableMapping/MutableSequence to check types instead of dict/list and use an explicit stack instead of recursion .

How to write assert where these dict will be equal?

 #!/usr/bin/env python3 import unittest class TestUnordered(unittest.TestCase): def test_equal_unordered(self): self.assertEqual(unordered('abc'), unordered('bac')) @unittest.expectedFailure def test_unequal_unordered(self): # for the correct error message self.addTypeEqualityFunc(unordered, 'assertCountEqual') self.assertEqual(unordered('abdc'), unordered('bac')) def test_nested_dict(self): make_list_unordered(STANDARD_DATA) make_list_unordered(UPDATED_DATA) self.assertEqual(STANDARD_DATA, UPDATED_DATA) if __name__ == "__main__": unittest.main() 

When you run this test, it displays:

 python test-nested-dict.py .Fx ====================================================================== FAIL: test_nested_dict (__main__.TestUnordered) ---------------------------------------------------------------------- Traceback (most recent call last): File "test-nested-dict.py", line 37, in test_nested_dict self.assertEqual(STANDARD_DATA, UPDATED_DATA) AssertionError: {'app[184 chars] '1.17', 'platform': 'IOS'}]}, {'application':[179 chars]}]}]} != {'app[184 chars] '1.18', 'platform': 'IOS'}]}, {'application':[179 chars]}]}]} Diff is 1270 characters long. Set self.maxDiff to None to see it. ---------------------------------------------------------------------- Ran 3 tests in 0.004s FAILED (failures=1, expected failures=1) 

What is expected, since '1.17' != '1.18' . If we correct the value of minimal , then the tests are successfully completed.

  • Thanks for the reply, fixed a typo - Name-name

I was able to find a suitable solution for a long time, thanks to the author of this gist https://gist.github.com/Back2Basics/0e6456de0395fd200a06

 import datetime, time, functools, operator, allure default_fudge = datetime.timedelta(seconds=0, microseconds=0, days=0) def deep_eq(_v1, _v2, datetime_fudge=default_fudge, _assert=True): """ Tests for deep equality between two python data structures recursing into sub-structures if necessary. Works with all python types including iterators and generators. This function was dreampt up to test API responses but could be used for anything. Be careful. With deeply nested structures you may blow the stack. Options: datetime_fudge => this is a datetime.timedelta object which, when comparing dates, will accept values that differ by the number of seconds specified _assert => passing yes for this will raise an assertion error when values do not match, instead of returning false (very useful in combination with pdb) """ _deep_eq = functools.partial(deep_eq, datetime_fudge=datetime_fudge, _assert=_assert) def _check_assert(R, a, b, reason=''): if _assert and not R: assert 0, "an assertion has failed in deep_eq (%s) %s != %s" % ( reason, str(a), str(b)) return R def _deep_dict_eq(d1, d2): k1, k2 = (sorted(d1.keys()), sorted(d2.keys())) if k1 != k2: # keys should be exactly equal return _check_assert(False, k1, k2, "keys") return _check_assert(operator.eq(sum(_deep_eq(d1[k], d2[k]) for k in k1), len(k1)), d1, d2, "dictionaries") def _deep_iter_eq(l1, l2): if len(l1) != len(l2): return _check_assert(False, l1, l2, "lengths") return _check_assert(operator.eq(sum(_deep_eq(v1, v2) for v1, v2 in zip(l1, l2)), len(l1)), l1, l2, "iterables") def op(a, b): _op = operator.eq if type(a) == datetime.datetime and type(b) == datetime.datetime: s = datetime_fudge.seconds t1, t2 = (time.mktime(a.timetuple()), time.mktime(b.timetuple())) l = t1 - t2 l = -l if l > 0 else l return _check_assert((-s if s > 0 else s) <= l, a, b, "dates") return _check_assert(_op(a, b), a, b, "values") c1, c2 = (_v1, _v2) # guard against strings because they are iterable and their # elements yield iterables infinitely. # INCEPTION if isinstance(_v1, str): pass else: if isinstance(_v1, dict): op = _deep_dict_eq else: try: c1, c2 = (list(iter(_v1)), list(iter(_v2))) except TypeError: c1, c2 = _v1, _v2 else: op = _deep_iter_eq return op(c1, c2) 

As a result, with _assert = True enabled, the problem is also displayed:

 R = False, a = '1.17', b = '1.19', reason = 'values' def _check_assert(R, a, b, reason=''): if _assert and not R: assert 0, "an assertion has failed in deep_eq (%s) %s != %s" % ( > reason, str(a), str(b)) E AssertionError: an assertion has failed in deep_eq (values) 1.17 != 1.19 
     def get_data(dt: dict) -> iter: apps = dt['applications'] for e, app in sorted(enumerate(d['application'] for d in apps), key=lambda a: a[1]): yield app, sorted(sorted(d.items()) for d in apps[e]['platforms']) assert list(get_data(STANDARD_DATA)) == list(get_data(UPDATED_DATA))