Skip to content

Commit fcfdd92

Browse files
committed
Improve performance and error readability of unittest.TestCase.assertDictEqual
1 parent 80d2a77 commit fcfdd92

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

‎Lib/test/test_unittest/test_assertions.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@ def testAssertNotIn(self):
267267

268268
def testAssertDictEqual(self):
269269
self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),
270-
[r"^\{\} != \{'key': 'value'\}\n\{\n Keys in the second dict but not the first:\n \+ 'key': 'value',\n\}$",
270+
[r"^\{\} != \{'key': 'value'\}\n\{\nKeys in the second dict but not the first:\n \+ 'key': 'value',\n\}$",
271271
r"^oops$",
272-
r"^\{\} != \{'key': 'value'\}\n\{\n Keys in the second dict but not the first:\n \+ 'key': 'value',\n\}$",
273-
r"^\{\} != \{'key': 'value'\}\n\{\n Keys in the second dict but not the first:\n \+ 'key': 'value',\n\} : oops$"])
272+
r"^\{\} != \{'key': 'value'\}\n\{\nKeys in the second dict but not the first:\n \+ 'key': 'value',\n\}$",
273+
r"^\{\} != \{'key': 'value'\}\n\{\nKeys in the second dict but not the first:\n \+ 'key': 'value',\n\} : oops$"])
274274

275275
def testAssertMultiLineEqual(self):
276276
self.assertMessages('assertMultiLineEqual', ("", "foo"),

‎Lib/unittest/case.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,19 +1230,18 @@ def _justified_values(d, keys, prefix):
12301230
commonvalues.append(key)
12311231
commonkeys.remove(key)
12321232
if commonvalues:
1233-
lines.append(" Keys in both dicts with identical values:")
12341233
lines.extend(_justified_values(d1, commonvalues, " "))
12351234
if commonkeys:
1236-
lines.append(" Keys in both dicts with differing values:")
1235+
lines.append("Keys in both dicts with differing values:")
12371236
for key in sorted(commonkeys):
12381237
key_repr = _value_repr(key)
12391238
lines.append(" - %s: %s," % (key_repr, _value_repr(d1[key])))
12401239
lines.append(" + %s: %s," % (key_repr, _value_repr(d2[key])))
12411240
if d1extrakeys:
1242-
lines.append(" Keys in the first dict but not the second:")
1241+
lines.append("Keys in the first dict but not the second:")
12431242
lines.extend(_justified_values(d1, d1extrakeys, "-"))
12441243
if d2extrakeys:
1245-
lines.append(" Keys in the second dict but not the first:")
1244+
lines.append("Keys in the second dict but not the first:")
12461245
lines.extend(_justified_values(d2, d2extrakeys, "+"))
12471246

12481247
diff = "\n{\n%s\n}" % '\n'.join(lines)

0 commit comments

Comments
 (0)