Index: Doc/library/unittest.rst =================================================================== --- Doc/library/unittest.rst (revision 87370) +++ Doc/library/unittest.rst (working copy) @@ -1138,37 +1138,37 @@ :meth:`.assertNotRegex`. - .. method:: assertDictContainsSubset(expected, actual, msg=None) + .. method:: assertDictContainsSubset(subset, dictionary, msg=None) - Tests whether the key/value pairs in dictionary *actual* are a - superset of those in *expected*. If not, an error message listing - the missing keys and mismatched values is generated. + Tests whether the key/value pairs in *dictionary* are a superset of + those in *subset*. If not, an error message listing the missing keys + and mismatched values is generated. .. versionadded:: 3.1 - .. method:: assertCountEqual(expected, actual, msg=None) + .. method:: assertCountEqual(actual, expected, msg=None) - Test that sequence *expected* contains the same elements as *actual*, + Test that sequence *actual* contains the same elements as *expected*, regardless of their order. When they don't, an error message listing the differences between the sequences will be generated. Duplicate elements are *not* ignored when comparing *actual* and *expected*. It verifies if each element has the same count in both sequences. Equivalent to: - ``assertEqual(Counter(iter(expected)), Counter(iter(actual)))`` + ``assertEqual(Counter(iter(actual)), Counter(iter(expected)))`` but works with sequences of unhashable objects as well. .. versionadded:: 3.2 .. method:: assertSameElements(actual, expected, msg=None) - Test that sequence *expected* contains the same elements as *actual*, + Test that sequence *actual* contains the same elements as *expected*, regardless of their order. When they don't, an error message listing the differences between the sequences will be generated. Duplicate elements are ignored when comparing *actual* and *expected*. - It is the equivalent of ``assertEqual(set(expected), set(actual))`` + It is the equivalent of ``assertEqual(set(actual), set(expected))`` but it works with sequences of unhashable objects as well. Because duplicates are ignored, this method has been deprecated in favour of :meth:`assertCountEqual`. @@ -1273,7 +1273,7 @@ .. versionadded:: 3.1 - .. method:: assertDictEqual(expected, actual, msg=None) + .. method:: assertDictEqual(dict1, dict2, msg=None) Test that two dictionaries are equal. If not, an error message is constructed that shows the differences in the dictionaries. This Index: Lib/unittest/case.py =================================================================== --- Lib/unittest/case.py (revision 87370) +++ Lib/unittest/case.py (working copy) @@ -904,17 +904,17 @@ standardMsg = self._truncateMessage(standardMsg, diff) self.fail(self._formatMessage(msg, standardMsg)) - def assertDictContainsSubset(self, expected, actual, msg=None): - """Checks whether actual is a superset of expected.""" + def assertDictContainsSubset(self, subset, dictionary, msg=None): + """Checks whether dictionary is a superset of subset.""" missing = [] mismatched = [] - for key, value in expected.items(): - if key not in actual: + for key, value in subset.items(): + if key not in dictionary: missing.append(key) - elif value != actual[key]: + elif value != dictionary[key]: mismatched.append('%s, expected: %s, actual: %s' % (safe_repr(key), safe_repr(value), - safe_repr(actual[key]))) + safe_repr(dictionary[key]))) if not (missing or mismatched): return @@ -973,13 +973,13 @@ self.fail(self._formatMessage(msg, standardMsg)) - def assertCountEqual(self, expected_seq, actual_seq, msg=None): + def assertCountEqual(self, actual_seq, expected_seq, msg=None): """An unordered sequence specific comparison. It asserts that - expected_seq and actual_seq have the same element counts. + actual_seq and expected_seq have the same element counts. Equivalent to:: - self.assertEqual(Counter(iter(expected_seq)), - Counter(iter(actual_seq))) + self.assertEqual(Counter(iter(actual_seq)), + Counter(iter(expected_seq))) Asserts that each element has the same count in both sequences. Example: @@ -987,15 +987,15 @@ - [0, 0, 1] and [0, 1] compare unequal. """ try: + actual = collections.Counter(iter(actual_seq)) expected = collections.Counter(iter(expected_seq)) - actual = collections.Counter(iter(actual_seq)) except TypeError: # Unsortable items (example: set(), complex(), ...) + actual = list(actual_seq) expected = list(expected_seq) - actual = list(actual_seq) missing, unexpected = unorderable_list_difference(expected, actual) else: - if expected == actual: + if actual == expected: return missing = list(expected - actual) unexpected = list(actual - expected)