mirror of
https://github.com/python/cpython.git
synced 2025-08-01 15:43:13 +00:00
#10573: use actual/expected consistently in unittest methods. The order of the args of assertCountEqual is also changed.
This commit is contained in:
parent
63563cdf9d
commit
addc6f5a21
3 changed files with 54 additions and 50 deletions
|
@ -848,12 +848,12 @@ Test cases
|
||||||
accept a *msg* argument that, if specified, is used as the error message on
|
accept a *msg* argument that, if specified, is used as the error message on
|
||||||
failure (see also :data:`longMessage`).
|
failure (see also :data:`longMessage`).
|
||||||
|
|
||||||
.. method:: assertEqual(first, second, msg=None)
|
.. method:: assertEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that *first* and *second* are equal. If the values do not compare
|
Test that *actual* and *expected* are equal. If the values do not
|
||||||
equal, the test will fail.
|
compare equal, the test will fail.
|
||||||
|
|
||||||
In addition, if *first* and *second* are the exact same type and one of
|
In addition, if *actual* and *expected* are the exact same type and one of
|
||||||
list, tuple, dict, set, frozenset or str or any type that a subclass
|
list, tuple, dict, set, frozenset or str or any type that a subclass
|
||||||
registers with :meth:`addTypeEqualityFunc` the type specific equality
|
registers with :meth:`addTypeEqualityFunc` the type specific equality
|
||||||
function will be called in order to generate a more useful default
|
function will be called in order to generate a more useful default
|
||||||
|
@ -868,10 +868,10 @@ Test cases
|
||||||
function for comparing strings.
|
function for comparing strings.
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertNotEqual(first, second, msg=None)
|
.. method:: assertNotEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that *first* and *second* are not equal. If the values do compare
|
Test that *actual* and *expected* are not equal. If the values do
|
||||||
equal, the test will fail.
|
compare equal, the test will fail.
|
||||||
|
|
||||||
.. method:: assertTrue(expr, msg=None)
|
.. method:: assertTrue(expr, msg=None)
|
||||||
assertFalse(expr, msg=None)
|
assertFalse(expr, msg=None)
|
||||||
|
@ -885,10 +885,11 @@ Test cases
|
||||||
provide a better error message in case of failure.
|
provide a better error message in case of failure.
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertIs(first, second, msg=None)
|
.. method:: assertIs(actual, expected, msg=None)
|
||||||
assertIsNot(first, second, msg=None)
|
assertIsNot(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that *first* and *second* evaluate (or don't evaluate) to the same object.
|
Test that *actual* and *expected* evaluate (or don't evaluate) to the
|
||||||
|
same object.
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
@ -1086,17 +1087,17 @@ Test cases
|
||||||
+---------------------------------------+--------------------------------+--------------+
|
+---------------------------------------+--------------------------------+--------------+
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertAlmostEqual(first, second, places=7, msg=None, delta=None)
|
.. method:: assertAlmostEqual(actual, expected, places=7, msg=None, delta=None)
|
||||||
assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
|
assertNotAlmostEqual(actual, expected, places=7, msg=None, delta=None)
|
||||||
|
|
||||||
Test that *first* and *second* are approximately (or not approximately)
|
Test that *actual* and *expected* are approximately (or not approximately)
|
||||||
equal by computing the difference, rounding to the given number of
|
equal by computing the difference, rounding to the given number of
|
||||||
decimal *places* (default 7), and comparing to zero. Note that these
|
decimal *places* (default 7), and comparing to zero. Note that these
|
||||||
methods round the values to the given number of *decimal places* (i.e.
|
methods round the values to the given number of *decimal places* (i.e.
|
||||||
like the :func:`round` function) and not *significant digits*.
|
like the :func:`round` function) and not *significant digits*.
|
||||||
|
|
||||||
If *delta* is supplied instead of *places* then the difference
|
If *delta* is supplied instead of *places* then the difference
|
||||||
between *first* and *second* must be less (or more) than *delta*.
|
between *actual* and *expected* must be less (or more) than *delta*.
|
||||||
|
|
||||||
Supplying both *delta* and *places* raises a ``TypeError``.
|
Supplying both *delta* and *places* raises a ``TypeError``.
|
||||||
|
|
||||||
|
@ -1106,12 +1107,12 @@ Test cases
|
||||||
if the objects compare equal. Added the *delta* keyword argument.
|
if the objects compare equal. Added the *delta* keyword argument.
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertGreater(first, second, msg=None)
|
.. method:: assertGreater(actual, expected, msg=None)
|
||||||
assertGreaterEqual(first, second, msg=None)
|
assertGreaterEqual(actual, expected, msg=None)
|
||||||
assertLess(first, second, msg=None)
|
assertLess(actual, expected, msg=None)
|
||||||
assertLessEqual(first, second, msg=None)
|
assertLessEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that *first* is respectively >, >=, < or <= than *second* depending
|
Test that *actual* is respectively >, >=, < or <= than *expected* depending
|
||||||
on the method name. If not, the test will fail::
|
on the method name. If not, the test will fail::
|
||||||
|
|
||||||
>>> self.assertGreaterEqual(3, 4)
|
>>> self.assertGreaterEqual(3, 4)
|
||||||
|
@ -1138,37 +1139,37 @@ Test cases
|
||||||
:meth:`.assertNotRegex`.
|
: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
|
Tests whether the key/value pairs in *dictionary* are a superset of
|
||||||
superset of those in *expected*. If not, an error message listing
|
those in *subset*. If not, an error message listing the missing keys
|
||||||
the missing keys and mismatched values is generated.
|
and mismatched values is generated.
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
.. 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
|
regardless of their order. When they don't, an error message listing the
|
||||||
differences between the sequences will be generated.
|
differences between the sequences will be generated.
|
||||||
|
|
||||||
Duplicate elements are *not* ignored when comparing *actual* and
|
Duplicate elements are *not* ignored when comparing *actual* and
|
||||||
*expected*. It verifies if each element has the same count in both
|
*expected*. It verifies if each element has the same count in both
|
||||||
sequences. Equivalent to:
|
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.
|
but works with sequences of unhashable objects as well.
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
.. method:: assertSameElements(actual, expected, msg=None)
|
.. 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
|
regardless of their order. When they don't, an error message listing
|
||||||
the differences between the sequences will be generated.
|
the differences between the sequences will be generated.
|
||||||
|
|
||||||
Duplicate elements are ignored when comparing *actual* and *expected*.
|
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
|
but it works with sequences of unhashable objects as well. Because
|
||||||
duplicates are ignored, this method has been deprecated in favour of
|
duplicates are ignored, this method has been deprecated in favour of
|
||||||
:meth:`assertCountEqual`.
|
:meth:`assertCountEqual`.
|
||||||
|
@ -1225,9 +1226,9 @@ Test cases
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertMultiLineEqual(first, second, msg=None)
|
.. method:: assertMultiLineEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that the multiline string *first* is equal to the string *second*.
|
Test that the multiline string *actual* is equal to the string *expected*.
|
||||||
When not equal a diff of the two strings highlighting the differences
|
When not equal a diff of the two strings highlighting the differences
|
||||||
will be included in the error message. This method is used by default
|
will be included in the error message. This method is used by default
|
||||||
when comparing strings with :meth:`assertEqual`.
|
when comparing strings with :meth:`assertEqual`.
|
||||||
|
@ -1235,10 +1236,10 @@ Test cases
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
|
.. method:: assertSequenceEqual(actual, expected, msg=None, seq_type=None)
|
||||||
|
|
||||||
Tests that two sequences are equal. If a *seq_type* is supplied, both
|
Tests that two sequences are equal. If a *seq_type* is supplied, both
|
||||||
*seq1* and *seq2* must be instances of *seq_type* or a failure will
|
*actual* and *expected* must be instances of *seq_type* or a failure will
|
||||||
be raised. If the sequences are different an error message is
|
be raised. If the sequences are different an error message is
|
||||||
constructed that shows the difference between the two.
|
constructed that shows the difference between the two.
|
||||||
|
|
||||||
|
@ -1249,8 +1250,8 @@ Test cases
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertListEqual(list1, list2, msg=None)
|
.. method:: assertListEqual(actual, expected, msg=None)
|
||||||
assertTupleEqual(tuple1, tuple2, msg=None)
|
assertTupleEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Tests that two lists or tuples are equal. If not an error message is
|
Tests that two lists or tuples are equal. If not an error message is
|
||||||
constructed that shows only the differences between the two. An error
|
constructed that shows only the differences between the two. An error
|
||||||
|
@ -1261,19 +1262,19 @@ Test cases
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertSetEqual(set1, set2, msg=None)
|
.. method:: assertSetEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Tests that two sets are equal. If not, an error message is constructed
|
Tests that two sets are equal. If not, an error message is constructed
|
||||||
that lists the differences between the sets. This method is used by
|
that lists the differences between the sets. This method is used by
|
||||||
default when comparing sets or frozensets with :meth:`assertEqual`.
|
default when comparing sets or frozensets with :meth:`assertEqual`.
|
||||||
|
|
||||||
Fails if either of *set1* or *set2* does not have a :meth:`set.difference`
|
Fails if either of *actual* or *expected* does not have a :meth:`set.difference`
|
||||||
method.
|
method.
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertDictEqual(expected, actual, msg=None)
|
.. method:: assertDictEqual(actual, expected, msg=None)
|
||||||
|
|
||||||
Test that two dictionaries are equal. If not, an error message is
|
Test that two dictionaries are equal. If not, an error message is
|
||||||
constructed that shows the differences in the dictionaries. This
|
constructed that shows the differences in the dictionaries. This
|
||||||
|
|
|
@ -904,17 +904,17 @@ class TestCase(object):
|
||||||
standardMsg = self._truncateMessage(standardMsg, diff)
|
standardMsg = self._truncateMessage(standardMsg, diff)
|
||||||
self.fail(self._formatMessage(msg, standardMsg))
|
self.fail(self._formatMessage(msg, standardMsg))
|
||||||
|
|
||||||
def assertDictContainsSubset(self, expected, actual, msg=None):
|
def assertDictContainsSubset(self, subset, dictionary, msg=None):
|
||||||
"""Checks whether actual is a superset of expected."""
|
"""Checks whether dictionary is a superset of subset."""
|
||||||
missing = []
|
missing = []
|
||||||
mismatched = []
|
mismatched = []
|
||||||
for key, value in expected.items():
|
for key, value in subset.items():
|
||||||
if key not in actual:
|
if key not in dictionary:
|
||||||
missing.append(key)
|
missing.append(key)
|
||||||
elif value != actual[key]:
|
elif value != dictionary[key]:
|
||||||
mismatched.append('%s, expected: %s, actual: %s' %
|
mismatched.append('%s, expected: %s, actual: %s' %
|
||||||
(safe_repr(key), safe_repr(value),
|
(safe_repr(key), safe_repr(value),
|
||||||
safe_repr(actual[key])))
|
safe_repr(dictionary[key])))
|
||||||
|
|
||||||
if not (missing or mismatched):
|
if not (missing or mismatched):
|
||||||
return
|
return
|
||||||
|
@ -973,13 +973,13 @@ class TestCase(object):
|
||||||
self.fail(self._formatMessage(msg, standardMsg))
|
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
|
"""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::
|
Equivalent to::
|
||||||
|
|
||||||
self.assertEqual(Counter(iter(expected_seq)),
|
self.assertEqual(Counter(iter(actual_seq)),
|
||||||
Counter(iter(actual_seq)))
|
Counter(iter(expected_seq)))
|
||||||
|
|
||||||
Asserts that each element has the same count in both sequences.
|
Asserts that each element has the same count in both sequences.
|
||||||
Example:
|
Example:
|
||||||
|
@ -987,15 +987,15 @@ class TestCase(object):
|
||||||
- [0, 0, 1] and [0, 1] compare unequal.
|
- [0, 0, 1] and [0, 1] compare unequal.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
expected = collections.Counter(iter(expected_seq))
|
|
||||||
actual = collections.Counter(iter(actual_seq))
|
actual = collections.Counter(iter(actual_seq))
|
||||||
|
expected = collections.Counter(iter(expected_seq))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# Unsortable items (example: set(), complex(), ...)
|
# Unsortable items (example: set(), complex(), ...)
|
||||||
expected = list(expected_seq)
|
|
||||||
actual = list(actual_seq)
|
actual = list(actual_seq)
|
||||||
|
expected = list(expected_seq)
|
||||||
missing, unexpected = unorderable_list_difference(expected, actual)
|
missing, unexpected = unorderable_list_difference(expected, actual)
|
||||||
else:
|
else:
|
||||||
if expected == actual:
|
if actual == expected:
|
||||||
return
|
return
|
||||||
missing = list(expected - actual)
|
missing = list(expected - actual)
|
||||||
unexpected = list(actual - expected)
|
unexpected = list(actual - expected)
|
||||||
|
|
|
@ -23,6 +23,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #10573: use actual/expected consistently in unittest methods.
|
||||||
|
The order of the args of assertCountEqual is also changed.
|
||||||
|
|
||||||
- Issue #9286: email.utils.parseaddr no longer concatenates blank-separated
|
- Issue #9286: email.utils.parseaddr no longer concatenates blank-separated
|
||||||
words in the local part of email addresses, thereby preserving the input.
|
words in the local part of email addresses, thereby preserving the input.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue