Improve assertJsonEqual error reporting
In the notification sample tests big json structures are asserted. If the difference is only in the number of keys in a dict or in the number of items in a list then the current assert only states the number of items. If the list or dict is big it is quite hard to see what is the missing key or item. This patch adds more descriptive output to the error message if the json structures are not equal. Error message without this patch: http://paste.openstack.org/show/616525/ Error message with this patch: http://paste.openstack.org/show/616526/ Change-Id: Iec15c9ea863db6bbe18afd64bd00a0780798cdf8
This commit is contained in:
parent
15500b89be
commit
a7851cf738
21
nova/test.py
21
nova/test.py
|
@ -29,6 +29,7 @@ import contextlib
|
|||
import copy
|
||||
import datetime
|
||||
import inspect
|
||||
import itertools
|
||||
import os
|
||||
import pprint
|
||||
import sys
|
||||
|
@ -431,7 +432,15 @@ class TestCase(testtools.TestCase):
|
|||
if isinstance(expected, dict) and isinstance(observed, dict):
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. Dict lengths are not equal' % path)
|
||||
('path: %s. Different dict key sets\n'
|
||||
'expected=%s\n'
|
||||
'observed=%s\n'
|
||||
'difference=%s') %
|
||||
(path,
|
||||
sorted(expected.keys()),
|
||||
sorted(observed.keys()),
|
||||
list(set(expected.keys()).symmetric_difference(
|
||||
set(observed.keys())))))
|
||||
expected_keys = sorted(expected)
|
||||
observed_keys = sorted(observed)
|
||||
self.assertEqual(
|
||||
|
@ -443,7 +452,15 @@ class TestCase(testtools.TestCase):
|
|||
isinstance(observed, (list, tuple, set))):
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. List lengths are not equal' % path)
|
||||
('path: %s. Different list items\n'
|
||||
'expected=%s\n'
|
||||
'observed=%s\n'
|
||||
'difference=%s') %
|
||||
(path,
|
||||
sorted(expected, key=sort_key),
|
||||
sorted(observed, key=sort_key),
|
||||
[a for a in itertools.chain(expected, observed) if
|
||||
(a not in expected) or (a not in observed)]))
|
||||
|
||||
expected_values_iter = iter(sorted(expected, key=sort_key))
|
||||
observed_values_iter = iter(sorted(observed, key=sort_key))
|
||||
|
|
|
@ -108,7 +108,10 @@ class JsonTestCase(test.NoDBTestCase):
|
|||
# error reported is going to be a cryptic length failure
|
||||
# on the level2 structure.
|
||||
self.assertEqual(
|
||||
"3 != 4: path: root.top.l1.l2. List lengths are not equal",
|
||||
("3 != 4: path: root.top.l1.l2. Different list items\n"
|
||||
"expected=['a', 'b', 'c']\n"
|
||||
"observed=['a', 'b', 'c', 'd']\n"
|
||||
"difference=['d']"),
|
||||
e.difference)
|
||||
self.assertIn(
|
||||
"actual:\n{'top': {'l1': {'l2': ['c', 'a', 'b', 'd']}}}",
|
||||
|
@ -138,7 +141,10 @@ class JsonTestCase(test.NoDBTestCase):
|
|||
self.assertJsonEqual(expected, actual)
|
||||
except Exception as e:
|
||||
self.assertEqual(
|
||||
"3 != 2: path: root.top.l1.l2. Dict lengths are not equal",
|
||||
("3 != 2: path: root.top.l1.l2. Different dict key sets\n"
|
||||
"expected=['a', 'b', 'c']\n"
|
||||
"observed=['a', 'b']\n"
|
||||
"difference=['c']"),
|
||||
e.difference)
|
||||
else:
|
||||
self.fail("This should have raised a mismatch exception")
|
||||
|
|
Loading…
Reference in New Issue