[issue10242] unittest's assertItemsEqual() method makes too many assumptions about its input
Raymond Hettinger
report at bugs.python.org
Thu Nov 4 00:06:48 CET 2010
Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:
Suggestions:
* new name: assertCountEqual(a, b)
or: assertElementCountEqual(a, b)
this name captures the essential service:
- unordered comparison where duplicates matter
- inputs are "elements",
not "items" which means key/value pairs
* O(n) implementation with O(n**2) fallback:
try:
a_cnt = collections.Counter(a)
b_cnt = collections.Counter(b)
except TypeError:
# do current O(n**2) fallback
else:
if a_cnt == b_cnt:
# test passed
else:
in_a_but_not_in_b = a - b
in_b_but_not_in_a = b - a
# display nice diff
* documentation should emphasize the new name:
assertElementCountEqual(a, b)
obsolete alias: assertItemsEqual(a, b)
----------
assignee: rhettinger -> michael.foord
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10242>
_______________________________________
More information about the Python-bugs-list
mailing list