Skip to content

Commit 17b4291

Browse files
Revert "bpo-45162: Revert "Remove many old deprecated unittest features" (GH-30935)"
This reverts commit b50322d.
1 parent d8104d1 commit 17b4291

13 files changed

+55
-373
lines changed

Doc/library/unittest.rst

+7-48
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,6 @@ Test cases
12611261
:meth:`.assertRegex`.
12621262
.. versionadded:: 3.2
12631263
:meth:`.assertNotRegex`.
1264-
.. versionadded:: 3.5
1265-
The name ``assertNotRegexpMatches`` is a deprecated alias
1266-
for :meth:`.assertNotRegex`.
12671264

12681265

12691266
.. method:: assertCountEqual(first, second, msg=None)
@@ -1660,40 +1657,6 @@ Test cases
16601657
:mod:`unittest`-based test framework.
16611658

16621659

1663-
.. _deprecated-aliases:
1664-
1665-
Deprecated aliases
1666-
##################
1667-
1668-
For historical reasons, some of the :class:`TestCase` methods had one or more
1669-
aliases that are now deprecated. The following table lists the correct names
1670-
along with their deprecated aliases:
1671-
1672-
============================== ====================== =======================
1673-
Method Name Deprecated alias Deprecated alias
1674-
============================== ====================== =======================
1675-
:meth:`.assertEqual` failUnlessEqual assertEquals
1676-
:meth:`.assertNotEqual` failIfEqual assertNotEquals
1677-
:meth:`.assertTrue` failUnless assert\_
1678-
:meth:`.assertFalse` failIf
1679-
:meth:`.assertRaises` failUnlessRaises
1680-
:meth:`.assertAlmostEqual` failUnlessAlmostEqual assertAlmostEquals
1681-
:meth:`.assertNotAlmostEqual` failIfAlmostEqual assertNotAlmostEquals
1682-
:meth:`.assertRegex` assertRegexpMatches
1683-
:meth:`.assertNotRegex` assertNotRegexpMatches
1684-
:meth:`.assertRaisesRegex` assertRaisesRegexp
1685-
============================== ====================== =======================
1686-
1687-
.. deprecated:: 3.1
1688-
The fail* aliases listed in the second column have been deprecated.
1689-
.. deprecated:: 3.2
1690-
The assert* aliases listed in the third column have been deprecated.
1691-
.. deprecated:: 3.2
1692-
``assertRegexpMatches`` and ``assertRaisesRegexp`` have been renamed to
1693-
:meth:`.assertRegex` and :meth:`.assertRaisesRegex`.
1694-
.. deprecated:: 3.5
1695-
The ``assertNotRegexpMatches`` name is deprecated in favor of :meth:`.assertNotRegex`.
1696-
16971660
.. _testsuite-objects:
16981661

16991662
Grouping tests
@@ -1819,7 +1782,7 @@ Loading and running tests
18191782
case is created for that method instead.
18201783

18211784

1822-
.. method:: loadTestsFromModule(module, pattern=None)
1785+
.. method:: loadTestsFromModule(module, *, pattern=None)
18231786

18241787
Return a suite of all test cases contained in the given module. This
18251788
method searches *module* for classes derived from :class:`TestCase` and
@@ -1843,10 +1806,11 @@ Loading and running tests
18431806
Support for ``load_tests`` added.
18441807

18451808
.. versionchanged:: 3.5
1846-
The undocumented and unofficial *use_load_tests* default argument is
1847-
deprecated and ignored, although it is still accepted for backward
1848-
compatibility. The method also now accepts a keyword-only argument
1849-
*pattern* which is passed to ``load_tests`` as the third argument.
1809+
Support for a keyword-only argument *pattern* has been added.
1810+
1811+
.. versionchanged:: 3.11
1812+
The undocumented and unofficial *use_load_tests* parameter has been
1813+
removed.
18501814

18511815

18521816
.. method:: loadTestsFromName(name, module=None)
@@ -2203,8 +2167,6 @@ Loading and running tests
22032167
:class:`TextTestRunner`.
22042168

22052169
.. versionadded:: 3.2
2206-
This class was previously named ``_TextTestResult``. The old name still
2207-
exists as an alias but is deprecated.
22082170

22092171

22102172
.. data:: defaultTestLoader
@@ -2227,10 +2189,7 @@ Loading and running tests
22272189
By default this runner shows :exc:`DeprecationWarning`,
22282190
:exc:`PendingDeprecationWarning`, :exc:`ResourceWarning` and
22292191
:exc:`ImportWarning` even if they are :ref:`ignored by default
2230-
<warning-ignored>`. Deprecation warnings caused by :ref:`deprecated unittest
2231-
methods <deprecated-aliases>` are also special-cased and, when the warning
2232-
filters are ``'default'`` or ``'always'``, they will appear only once
2233-
per-module, in order to avoid too many warning messages. This behavior can
2192+
<warning-ignored>`. This behavior can
22342193
be overridden using Python's :option:`!-Wd` or :option:`!-Wa` options
22352194
(see :ref:`Warning control <using-on-warnings>`) and leaving
22362195
*warnings* to ``None``.

Doc/whatsnew/3.11.rst

+22
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,28 @@ Removed
13171317
and :class:`fileinput.FileInput`, deprecated since Python 3.9.
13181318
(Contributed by Hugo van Kemenade in :issue:`45132`.)
13191319

1320+
* Removed many old deprecated :mod:`unittest` features:
1321+
1322+
- :class:`~unittest.TestCase` method aliases ``failUnlessEqual``,
1323+
``failIfEqual``, ``failUnless``, ``failIf``, ``failUnlessRaises``,
1324+
``failUnlessAlmostEqual``, ``failIfAlmostEqual`` (deprecated in Python 3.1),
1325+
``assertEquals``, ``assertNotEquals``, ``assert_``, ``assertAlmostEquals``,
1326+
``assertNotAlmostEquals``, ``assertRegexpMatches``, ``assertRaisesRegexp``
1327+
(deprecated in Python 3.2), and ``assertNotRegexpMatches`` (deprecated in
1328+
Python 3.5).
1329+
1330+
- Undocumented and broken :class:`~unittest.TestCase` method
1331+
``assertDictContainsSubset`` (deprecated in Python 3.2).
1332+
1333+
- Undocumented :meth:`<unittest.TestLoader.loadTestsFromModule>
1334+
TestLoader.loadTestsFromModule` parameter *use_load_tests* (deprecated
1335+
and ignored since Python 3.2).
1336+
1337+
- An alias of the :class:`~unittest.TextTestResult` class:
1338+
``_TextTestResult`` (deprecated in Python 3.2).
1339+
1340+
(Contributed by Serhiy Storchaka in :issue:`45162`.)
1341+
13201342
* The following deprecated functions and methods are removed in the :mod:`gettext`
13211343
module: :func:`~gettext.lgettext`, :func:`~gettext.ldgettext`,
13221344
:func:`~gettext.lngettext` and :func:`~gettext.ldngettext`.

Doc/whatsnew/3.2.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -1815,8 +1815,7 @@ names.
18151815
=============================== ==============================
18161816

18171817
Likewise, the ``TestCase.fail*`` methods deprecated in Python 3.1 are expected
1818-
to be removed in Python 3.3. Also see the :ref:`deprecated-aliases` section in
1819-
the :mod:`unittest` documentation.
1818+
to be removed in Python 3.3.
18201819

18211820
(Contributed by Ezio Melotti; :issue:`9424`.)
18221821

Lib/unittest/__init__.py

-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ def testMultiply(self):
6969
# IsolatedAsyncioTestCase will be imported lazily.
7070
from .loader import makeSuite, getTestCaseNames, findTestCases
7171

72-
# deprecated
73-
_TextTestResult = TextTestResult
74-
7572

7673
# There are no tests here, so don't try to run anything discovered from
7774
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our

Lib/unittest/case.py

-50
Original file line numberDiff line numberDiff line change
@@ -1171,35 +1171,6 @@ def assertDictEqual(self, d1, d2, msg=None):
11711171
standardMsg = self._truncateMessage(standardMsg, diff)
11721172
self.fail(self._formatMessage(msg, standardMsg))
11731173

1174-
def assertDictContainsSubset(self, subset, dictionary, msg=None):
1175-
"""Checks whether dictionary is a superset of subset."""
1176-
warnings.warn('assertDictContainsSubset is deprecated',
1177-
DeprecationWarning)
1178-
missing = []
1179-
mismatched = []
1180-
for key, value in subset.items():
1181-
if key not in dictionary:
1182-
missing.append(key)
1183-
elif value != dictionary[key]:
1184-
mismatched.append('%s, expected: %s, actual: %s' %
1185-
(safe_repr(key), safe_repr(value),
1186-
safe_repr(dictionary[key])))
1187-
1188-
if not (missing or mismatched):
1189-
return
1190-
1191-
standardMsg = ''
1192-
if missing:
1193-
standardMsg = 'Missing: %s' % ','.join(safe_repr(m) for m in
1194-
missing)
1195-
if mismatched:
1196-
if standardMsg:
1197-
standardMsg += '; '
1198-
standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
1199-
1200-
self.fail(self._formatMessage(msg, standardMsg))
1201-
1202-
12031174
def assertCountEqual(self, first, second, msg=None):
12041175
"""Asserts that two iterables have the same elements, the same number of
12051176
times, without regard to order.
@@ -1363,27 +1334,6 @@ def assertNotRegex(self, text, unexpected_regex, msg=None):
13631334
raise self.failureException(msg)
13641335

13651336

1366-
def _deprecate(original_func):
1367-
def deprecated_func(*args, **kwargs):
1368-
warnings.warn(
1369-
'Please use {0} instead.'.format(original_func.__name__),
1370-
DeprecationWarning, 2)
1371-
return original_func(*args, **kwargs)
1372-
return deprecated_func
1373-
1374-
# see #9424
1375-
failUnlessEqual = assertEquals = _deprecate(assertEqual)
1376-
failIfEqual = assertNotEquals = _deprecate(assertNotEqual)
1377-
failUnlessAlmostEqual = assertAlmostEquals = _deprecate(assertAlmostEqual)
1378-
failIfAlmostEqual = assertNotAlmostEquals = _deprecate(assertNotAlmostEqual)
1379-
failUnless = assert_ = _deprecate(assertTrue)
1380-
failUnlessRaises = _deprecate(assertRaises)
1381-
failIf = _deprecate(assertFalse)
1382-
assertRaisesRegexp = _deprecate(assertRaisesRegex)
1383-
assertRegexpMatches = _deprecate(assertRegex)
1384-
assertNotRegexpMatches = _deprecate(assertNotRegex)
1385-
1386-
13871337

13881338
class FunctionTestCase(TestCase):
13891339
"""A test case that wraps a test function.

Lib/unittest/loader.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,8 @@ def loadTestsFromTestCase(self, testCaseClass):
9393
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
9494
return loaded_suite
9595

96-
# XXX After Python 3.5, remove backward compatibility hacks for
97-
# use_load_tests deprecation via *args and **kws. See issue 16662.
98-
def loadTestsFromModule(self, module, *args, pattern=None, **kws):
96+
def loadTestsFromModule(self, module, *, pattern=None):
9997
"""Return a suite of all test cases contained in the given module"""
100-
# This method used to take an undocumented and unofficial
101-
# use_load_tests argument. For backward compatibility, we still
102-
# accept the argument (which can also be the first position) but we
103-
# ignore it and issue a deprecation warning if it's present.
104-
if len(args) > 0 or 'use_load_tests' in kws:
105-
warnings.warn('use_load_tests is deprecated and ignored',
106-
DeprecationWarning)
107-
kws.pop('use_load_tests', None)
108-
if len(args) > 1:
109-
# Complain about the number of arguments, but don't forget the
110-
# required `module` argument.
111-
complaint = len(args) + 1
112-
raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
113-
if len(kws) != 0:
114-
# Since the keyword arguments are unsorted (see PEP 468), just
115-
# pick the alphabetically sorted first argument to complain about,
116-
# if multiple were given. At least the error message will be
117-
# predictable.
118-
complaint = sorted(kws)[0]
119-
raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
12098
tests = []
12199
for name in dir(module):
122100
obj = getattr(module, name)

Lib/unittest/runner.py

-9
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,6 @@ def run(self, test):
200200
if self.warnings:
201201
# if self.warnings is set, use it to filter all the warnings
202202
warnings.simplefilter(self.warnings)
203-
# if the filter is 'default' or 'always', special-case the
204-
# warnings from the deprecated unittest methods to show them
205-
# no more than once per module, because they can be fairly
206-
# noisy. The -Wd and -Wa flags can be used to bypass this
207-
# only when self.warnings is None.
208-
if self.warnings in ['default', 'always']:
209-
warnings.filterwarnings('module',
210-
category=DeprecationWarning,
211-
message=r'Please use assert\w+ instead.')
212203
startTime = time.perf_counter()
213204
startTestRun = getattr(result, 'startTestRun', None)
214205
if startTestRun is not None:

Lib/unittest/test/_test_warnings.py

-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ def warnfun():
1818
warnings.warn('rw', RuntimeWarning)
1919

2020
class TestWarnings(unittest.TestCase):
21-
# unittest warnings will be printed at most once per type (max one message
22-
# for the fail* methods, and one for the assert* methods)
23-
def test_assert(self):
24-
self.assertEquals(2+2, 4)
25-
self.assertEquals(2*2, 4)
26-
self.assertEquals(2**2, 4)
27-
28-
def test_fail(self):
29-
self.failUnless(1)
30-
self.failUnless(True)
31-
3221
def test_other_unittest(self):
3322
self.assertAlmostEqual(2+2, 4)
3423
self.assertNotAlmostEqual(4+4, 2)

Lib/unittest/test/test_assertions.py

-9
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,6 @@ def testAssertDictEqual(self):
271271
r"\+ \{'key': 'value'\}$",
272272
r"\+ \{'key': 'value'\} : oops$"])
273273

274-
def testAssertDictContainsSubset(self):
275-
with warnings.catch_warnings():
276-
warnings.simplefilter("ignore", DeprecationWarning)
277-
278-
self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
279-
["^Missing: 'key'$", "^oops$",
280-
"^Missing: 'key'$",
281-
"^Missing: 'key' : oops$"])
282-
283274
def testAssertMultiLineEqual(self):
284275
self.assertMessages('assertMultiLineEqual', ("", "foo"),
285276
[r"\+ foo$", "^oops$",

Lib/unittest/test/test_case.py

+6-63
Original file line numberDiff line numberDiff line change
@@ -698,36 +698,6 @@ def testAssertIn(self):
698698
self.assertRaises(self.failureException, self.assertNotIn, 'cow',
699699
animals)
700700

701-
def testAssertDictContainsSubset(self):
702-
with warnings.catch_warnings():
703-
warnings.simplefilter("ignore", DeprecationWarning)
704-
705-
self.assertDictContainsSubset({}, {})
706-
self.assertDictContainsSubset({}, {'a': 1})
707-
self.assertDictContainsSubset({'a': 1}, {'a': 1})
708-
self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
709-
self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
710-
711-
with self.assertRaises(self.failureException):
712-
self.assertDictContainsSubset({1: "one"}, {})
713-
714-
with self.assertRaises(self.failureException):
715-
self.assertDictContainsSubset({'a': 2}, {'a': 1})
716-
717-
with self.assertRaises(self.failureException):
718-
self.assertDictContainsSubset({'c': 1}, {'a': 1})
719-
720-
with self.assertRaises(self.failureException):
721-
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
722-
723-
with self.assertRaises(self.failureException):
724-
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
725-
726-
one = ''.join(chr(i) for i in range(255))
727-
# this used to cause a UnicodeDecodeError constructing the failure msg
728-
with self.assertRaises(self.failureException):
729-
self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'})
730-
731701
def testAssertEqual(self):
732702
equal_pairs = [
733703
((), ()),
@@ -1790,45 +1760,18 @@ def testAssertNoLogsYieldsNone(self):
17901760
pass
17911761
self.assertIsNone(value)
17921762

1793-
def testDeprecatedMethodNames(self):
1794-
"""
1795-
Test that the deprecated methods raise a DeprecationWarning. See #9424.
1796-
"""
1797-
old = (
1798-
(self.failIfEqual, (3, 5)),
1799-
(self.assertNotEquals, (3, 5)),
1800-
(self.failUnlessEqual, (3, 3)),
1801-
(self.assertEquals, (3, 3)),
1802-
(self.failUnlessAlmostEqual, (2.0, 2.0)),
1803-
(self.assertAlmostEquals, (2.0, 2.0)),
1804-
(self.failIfAlmostEqual, (3.0, 5.0)),
1805-
(self.assertNotAlmostEquals, (3.0, 5.0)),
1806-
(self.failUnless, (True,)),
1807-
(self.assert_, (True,)),
1808-
(self.failUnlessRaises, (TypeError, lambda _: 3.14 + 'spam')),
1809-
(self.failIf, (False,)),
1810-
(self.assertDictContainsSubset, (dict(a=1, b=2), dict(a=1, b=2, c=3))),
1811-
(self.assertRaisesRegexp, (KeyError, 'foo', lambda: {}['foo'])),
1812-
(self.assertRegexpMatches, ('bar', 'bar')),
1813-
)
1814-
for meth, args in old:
1815-
with self.assertWarns(DeprecationWarning):
1816-
meth(*args)
1817-
1818-
# disable this test for now. When the version where the fail* methods will
1819-
# be removed is decided, re-enable it and update the version
1820-
def _testDeprecatedFailMethods(self):
1821-
"""Test that the deprecated fail* methods get removed in 3.x"""
1822-
if sys.version_info[:2] < (3, 3):
1823-
return
1763+
def testDeprecatedFailMethods(self):
1764+
"""Test that the deprecated fail* methods get removed in 3.11"""
18241765
deprecated_names = [
18251766
'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual',
18261767
'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf',
1827-
'assertDictContainsSubset',
1768+
'assertNotEquals', 'assertEquals', 'assertAlmostEquals',
1769+
'assertNotAlmostEquals', 'assert_', 'assertDictContainsSubset',
1770+
'assertRaisesRegexp', 'assertRegexpMatches'
18281771
]
18291772
for deprecated_name in deprecated_names:
18301773
with self.assertRaises(AttributeError):
1831-
getattr(self, deprecated_name) # remove these in 3.x
1774+
getattr(self, deprecated_name)
18321775

18331776
def testDeepcopy(self):
18341777
# Issue: 5660

0 commit comments

Comments
 (0)