Skip to content

Commit 10d7b6e

Browse files
committed
Merge bitcoin/bitcoin#29514: tests: Provide more helpful assert_equal errors
a3badf7 tests: Provide more helpful assert_equal errors (Anthony Towns) Pull request description: In the functional tests, we often compare dicts with assert_equal, but the output makes it very hard to tell exactly which entry in the dicts don't match when there are a lot of entries and only minor differences. Change the output to make it clearer. ACKs for top commit: achow101: ACK a3badf7 vasild: ACK a3badf7 brunoerg: utACK a3badf7 josibake: ACK bitcoin/bitcoin@a3badf7 BrandonOdiwuor: Code Review ACK a3badf7 Tree-SHA512: 1d4b4a3b2e2e28ab09f10b41b04b52b37f64e0d8a54e2306f37de0c3eb3299a7ad4ba225b9efa67057a75e90d008a17385c810a32c9b212d240be280c2dcf2e5
2 parents 5ebb406 + a3badf7 commit 10d7b6e

File tree

1 file changed

+17
-0
lines changed
  • test/functional/test_framework

1 file changed

+17
-0
lines changed

test/functional/test_framework/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,24 @@ def assert_fee_amount(fee, tx_size, feerate_BTC_kvB):
5252
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)" % (str(fee), str(target_fee)))
5353

5454

55+
def summarise_dict_differences(thing1, thing2):
56+
if not isinstance(thing1, dict) or not isinstance(thing2, dict):
57+
return thing1, thing2
58+
d1, d2 = {}, {}
59+
for k in sorted(thing1.keys()):
60+
if k not in thing2:
61+
d1[k] = thing1[k]
62+
elif thing1[k] != thing2[k]:
63+
d1[k], d2[k] = summarise_dict_differences(thing1[k], thing2[k])
64+
for k in sorted(thing2.keys()):
65+
if k not in thing1:
66+
d2[k] = thing2[k]
67+
return d1, d2
68+
5569
def assert_equal(thing1, thing2, *args):
70+
if thing1 != thing2 and not args and isinstance(thing1, dict) and isinstance(thing2, dict):
71+
d1,d2 = summarise_dict_differences(thing1, thing2)
72+
raise AssertionError("not(%s == %s)\n in particular not(%s == %s)" % (thing1, thing2, d1, d2))
5673
if thing1 != thing2 or any(thing1 != arg for arg in args):
5774
raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
5875

0 commit comments

Comments
 (0)