Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit addc6f5

Browse files
committed
#10573: use actual/expected consistently in unittest methods. The order of the args of assertCountEqual is also changed.
1 parent 63563cd commit addc6f5

3 files changed

Lines changed: 54 additions & 50 deletions

File tree

Doc/library/unittest.rst

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,12 @@ Test cases
848848
accept a *msg* argument that, if specified, is used as the error message on
849849
failure (see also :data:`longMessage`).
850850

851-
.. method:: assertEqual(first, second, msg=None)
851+
.. method:: assertEqual(actual, expected, msg=None)
852852

853-
Test that *first* and *second* are equal. If the values do not compare
854-
equal, the test will fail.
853+
Test that *actual* and *expected* are equal. If the values do not
854+
compare equal, the test will fail.
855855

856-
In addition, if *first* and *second* are the exact same type and one of
856+
In addition, if *actual* and *expected* are the exact same type and one of
857857
list, tuple, dict, set, frozenset or str or any type that a subclass
858858
registers with :meth:`addTypeEqualityFunc` the type specific equality
859859
function will be called in order to generate a more useful default
@@ -868,10 +868,10 @@ Test cases
868868
function for comparing strings.
869869

870870

871-
.. method:: assertNotEqual(first, second, msg=None)
871+
.. method:: assertNotEqual(actual, expected, msg=None)
872872

873-
Test that *first* and *second* are not equal. If the values do compare
874-
equal, the test will fail.
873+
Test that *actual* and *expected* are not equal. If the values do
874+
compare equal, the test will fail.
875875

876876
.. method:: assertTrue(expr, msg=None)
877877
assertFalse(expr, msg=None)
@@ -885,10 +885,11 @@ Test cases
885885
provide a better error message in case of failure.
886886

887887

888-
.. method:: assertIs(first, second, msg=None)
889-
assertIsNot(first, second, msg=None)
888+
.. method:: assertIs(actual, expected, msg=None)
889+
assertIsNot(actual, expected, msg=None)
890890

891-
Test that *first* and *second* evaluate (or don't evaluate) to the same object.
891+
Test that *actual* and *expected* evaluate (or don't evaluate) to the
892+
same object.
892893

893894
.. versionadded:: 3.1
894895

@@ -1086,17 +1087,17 @@ Test cases
10861087
+---------------------------------------+--------------------------------+--------------+
10871088

10881089

1089-
.. method:: assertAlmostEqual(first, second, places=7, msg=None, delta=None)
1090-
assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
1090+
.. method:: assertAlmostEqual(actual, expected, places=7, msg=None, delta=None)
1091+
assertNotAlmostEqual(actual, expected, places=7, msg=None, delta=None)
10911092

1092-
Test that *first* and *second* are approximately (or not approximately)
1093+
Test that *actual* and *expected* are approximately (or not approximately)
10931094
equal by computing the difference, rounding to the given number of
10941095
decimal *places* (default 7), and comparing to zero. Note that these
10951096
methods round the values to the given number of *decimal places* (i.e.
10961097
like the :func:`round` function) and not *significant digits*.
10971098

10981099
If *delta* is supplied instead of *places* then the difference
1099-
between *first* and *second* must be less (or more) than *delta*.
1100+
between *actual* and *expected* must be less (or more) than *delta*.
11001101

11011102
Supplying both *delta* and *places* raises a ``TypeError``.
11021103

@@ -1106,12 +1107,12 @@ Test cases
11061107
if the objects compare equal. Added the *delta* keyword argument.
11071108

11081109

1109-
.. method:: assertGreater(first, second, msg=None)
1110-
assertGreaterEqual(first, second, msg=None)
1111-
assertLess(first, second, msg=None)
1112-
assertLessEqual(first, second, msg=None)
1110+
.. method:: assertGreater(actual, expected, msg=None)
1111+
assertGreaterEqual(actual, expected, msg=None)
1112+
assertLess(actual, expected, msg=None)
1113+
assertLessEqual(actual, expected, msg=None)
11131114

1114-
Test that *first* is respectively >, >=, < or <= than *second* depending
1115+
Test that *actual* is respectively >, >=, < or <= than *expected* depending
11151116
on the method name. If not, the test will fail::
11161117

11171118
>>> self.assertGreaterEqual(3, 4)
@@ -1138,37 +1139,37 @@ Test cases
11381139
:meth:`.assertNotRegex`.
11391140

11401141

1141-
.. method:: assertDictContainsSubset(expected, actual, msg=None)
1142+
.. method:: assertDictContainsSubset(subset, dictionary, msg=None)
11421143

1143-
Tests whether the key/value pairs in dictionary *actual* are a
1144-
superset of those in *expected*. If not, an error message listing
1145-
the missing keys and mismatched values is generated.
1144+
Tests whether the key/value pairs in *dictionary* are a superset of
1145+
those in *subset*. If not, an error message listing the missing keys
1146+
and mismatched values is generated.
11461147

11471148
.. versionadded:: 3.1
11481149

11491150

1150-
.. method:: assertCountEqual(expected, actual, msg=None)
1151+
.. method:: assertCountEqual(actual, expected, msg=None)
11511152

1152-
Test that sequence *expected* contains the same elements as *actual*,
1153+
Test that sequence *actual* contains the same elements as *expected*,
11531154
regardless of their order. When they don't, an error message listing the
11541155
differences between the sequences will be generated.
11551156

11561157
Duplicate elements are *not* ignored when comparing *actual* and
11571158
*expected*. It verifies if each element has the same count in both
11581159
sequences. Equivalent to:
1159-
``assertEqual(Counter(iter(expected)), Counter(iter(actual)))``
1160+
``assertEqual(Counter(iter(actual)), Counter(iter(expected)))``
11601161
but works with sequences of unhashable objects as well.
11611162

11621163
.. versionadded:: 3.2
11631164

11641165
.. method:: assertSameElements(actual, expected, msg=None)
11651166

1166-
Test that sequence *expected* contains the same elements as *actual*,
1167+
Test that sequence *actual* contains the same elements as *expected*,
11671168
regardless of their order. When they don't, an error message listing
11681169
the differences between the sequences will be generated.
11691170

11701171
Duplicate elements are ignored when comparing *actual* and *expected*.
1171-
It is the equivalent of ``assertEqual(set(expected), set(actual))``
1172+
It is the equivalent of ``assertEqual(set(actual), set(expected))``
11721173
but it works with sequences of unhashable objects as well. Because
11731174
duplicates are ignored, this method has been deprecated in favour of
11741175
:meth:`assertCountEqual`.
@@ -1225,20 +1226,20 @@ Test cases
12251226

12261227

12271228

1228-
.. method:: assertMultiLineEqual(first, second, msg=None)
1229+
.. method:: assertMultiLineEqual(actual, expected, msg=None)
12291230

1230-
Test that the multiline string *first* is equal to the string *second*.
1231+
Test that the multiline string *actual* is equal to the string *expected*.
12311232
When not equal a diff of the two strings highlighting the differences
12321233
will be included in the error message. This method is used by default
12331234
when comparing strings with :meth:`assertEqual`.
12341235

12351236
.. versionadded:: 3.1
12361237

12371238

1238-
.. method:: assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
1239+
.. method:: assertSequenceEqual(actual, expected, msg=None, seq_type=None)
12391240

12401241
Tests that two sequences are equal. If a *seq_type* is supplied, both
1241-
*seq1* and *seq2* must be instances of *seq_type* or a failure will
1242+
*actual* and *expected* must be instances of *seq_type* or a failure will
12421243
be raised. If the sequences are different an error message is
12431244
constructed that shows the difference between the two.
12441245

@@ -1249,8 +1250,8 @@ Test cases
12491250
.. versionadded:: 3.1
12501251

12511252

1252-
.. method:: assertListEqual(list1, list2, msg=None)
1253-
assertTupleEqual(tuple1, tuple2, msg=None)
1253+
.. method:: assertListEqual(actual, expected, msg=None)
1254+
assertTupleEqual(actual, expected, msg=None)
12541255

12551256
Tests that two lists or tuples are equal. If not an error message is
12561257
constructed that shows only the differences between the two. An error
@@ -1261,19 +1262,19 @@ Test cases
12611262
.. versionadded:: 3.1
12621263

12631264

1264-
.. method:: assertSetEqual(set1, set2, msg=None)
1265+
.. method:: assertSetEqual(actual, expected, msg=None)
12651266

12661267
Tests that two sets are equal. If not, an error message is constructed
12671268
that lists the differences between the sets. This method is used by
12681269
default when comparing sets or frozensets with :meth:`assertEqual`.
12691270

1270-
Fails if either of *set1* or *set2* does not have a :meth:`set.difference`
1271+
Fails if either of *actual* or *expected* does not have a :meth:`set.difference`
12711272
method.
12721273

12731274
.. versionadded:: 3.1
12741275

12751276

1276-
.. method:: assertDictEqual(expected, actual, msg=None)
1277+
.. method:: assertDictEqual(actual, expected, msg=None)
12771278

12781279
Test that two dictionaries are equal. If not, an error message is
12791280
constructed that shows the differences in the dictionaries. This

Lib/unittest/case.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -904,17 +904,17 @@ def assertDictEqual(self, d1, d2, msg=None):
904904
standardMsg = self._truncateMessage(standardMsg, diff)
905905
self.fail(self._formatMessage(msg, standardMsg))
906906

907-
def assertDictContainsSubset(self, expected, actual, msg=None):
908-
"""Checks whether actual is a superset of expected."""
907+
def assertDictContainsSubset(self, subset, dictionary, msg=None):
908+
"""Checks whether dictionary is a superset of subset."""
909909
missing = []
910910
mismatched = []
911-
for key, value in expected.items():
912-
if key not in actual:
911+
for key, value in subset.items():
912+
if key not in dictionary:
913913
missing.append(key)
914-
elif value != actual[key]:
914+
elif value != dictionary[key]:
915915
mismatched.append('%s, expected: %s, actual: %s' %
916916
(safe_repr(key), safe_repr(value),
917-
safe_repr(actual[key])))
917+
safe_repr(dictionary[key])))
918918

919919
if not (missing or mismatched):
920920
return
@@ -973,29 +973,29 @@ def assertSameElements(self, expected_seq, actual_seq, msg=None):
973973
self.fail(self._formatMessage(msg, standardMsg))
974974

975975

976-
def assertCountEqual(self, expected_seq, actual_seq, msg=None):
976+
def assertCountEqual(self, actual_seq, expected_seq, msg=None):
977977
"""An unordered sequence specific comparison. It asserts that
978-
expected_seq and actual_seq have the same element counts.
978+
actual_seq and expected_seq have the same element counts.
979979
Equivalent to::
980980
981-
self.assertEqual(Counter(iter(expected_seq)),
982-
Counter(iter(actual_seq)))
981+
self.assertEqual(Counter(iter(actual_seq)),
982+
Counter(iter(expected_seq)))
983983
984984
Asserts that each element has the same count in both sequences.
985985
Example:
986986
- [0, 1, 1] and [1, 0, 1] compare equal.
987987
- [0, 0, 1] and [0, 1] compare unequal.
988988
"""
989989
try:
990-
expected = collections.Counter(iter(expected_seq))
991990
actual = collections.Counter(iter(actual_seq))
991+
expected = collections.Counter(iter(expected_seq))
992992
except TypeError:
993993
# Unsortable items (example: set(), complex(), ...)
994-
expected = list(expected_seq)
995994
actual = list(actual_seq)
995+
expected = list(expected_seq)
996996
missing, unexpected = unorderable_list_difference(expected, actual)
997997
else:
998-
if expected == actual:
998+
if actual == expected:
999999
return
10001000
missing = list(expected - actual)
10011001
unexpected = list(actual - expected)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- Issue #10573: use actual/expected consistently in unittest methods.
27+
The order of the args of assertCountEqual is also changed.
28+
2629
- Issue #9286: email.utils.parseaddr no longer concatenates blank-separated
2730
words in the local part of email addresses, thereby preserving the input.
2831

0 commit comments

Comments
 (0)