summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/case.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-03-20 16:58:04 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-03-20 16:58:04 (GMT)
commit8442a606b82683121fc38b462f54754e22be3535 (patch)
tree3ea3861e6e50d86a0ce57bddfb3474c2156d7ad4 /Lib/unittest/case.py
parenta087963578b29e7360564e87d7ba703eabbd6487 (diff)
downloadcpython-8442a606b82683121fc38b462f54754e22be3535.zip
cpython-8442a606b82683121fc38b462f54754e22be3535.tar.gz
cpython-8442a606b82683121fc38b462f54754e22be3535.tar.bz2
Adding assertItemsEqual with tests. Issue 7832. assertSameElements still needs to be deprecated plus documentation needs to be updated.
Diffstat (limited to 'Lib/unittest/case.py')
-rw-r--r--Lib/unittest/case.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 8900915..d9f35fe 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -838,6 +838,44 @@ class TestCase(object):
standardMsg = '\n'.join(errors)
self.fail(self._formatMessage(msg, standardMsg))
+
+ def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
+ """An unordered sequence / set specific comparison. It asserts that
+ expected_seq and actual_seq contain the same elements. It is
+ the equivalent of::
+
+ self.assertEqual(sorted(expected_seq), sorted(actual_seq))
+
+ Raises with an error message listing which elements of expected_seq
+ are missing from actual_seq and vice versa if any.
+
+ Asserts that each element has the same count in both sequences.
+ Example:
+ - [0, 1, 1] and [1, 0, 1] compare equal.
+ - [0, 0, 1] and [0, 1] compare unequal.
+ """
+ try:
+ expected = sorted(expected_seq)
+ actual = sorted(actual_seq)
+ except TypeError:
+ # Unsortable items (example: set(), complex(), ...)
+ expected = list(expected_seq)
+ actual = list(actual_seq)
+ missing, unexpected = unorderable_list_difference(expected, actual)
+ else:
+ return self.assertSequenceEqual(expected, actual, msg=msg)
+
+ errors = []
+ if missing:
+ errors.append('Expected, but missing:\n %s' %
+ safe_repr(missing))
+ if unexpected:
+ errors.append('Unexpected, but present:\n %s' %
+ safe_repr(unexpected))
+ if errors:
+ standardMsg = '\n'.join(errors)
+ self.fail(self._formatMessage(msg, standardMsg))
+
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assert_(isinstance(first, str), (