8000 Revert "bpo-45162: Revert "Remove many old deprecated unittest featur… · python/cpython@c834c02 · GitHub
[go: up one dir, main page]

Skip to content

Commit c834c02

Browse files
Revert "bpo-45162: Revert "Remove many old deprecated unittest features"" (GH-92556)
This reverts commit b50322d.
1 parent 38612a0 commit c834c02

File tree

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.12
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 onl F438 y 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``.
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,45 @@ although there is currently no date scheduled for their removal.
178178
Removed
179179
=======
180180

181+
* Removed many old deprecated :mod:`unittest` features:
182+
183+
- A number of :class:`~unittest.TestCase` method aliases:
184+
185+
============================ =============================== ===============
186+
Deprecated alias Method Name Deprecated in
187+
============================ =============================== ===============
188+
``failUnless`` :meth:`.assertTrue` 3.1
189+
``failIf`` :meth:`.assertFalse` 3.1
190+
``failUnlessEqual`` :meth:`.assertEqual` 3.1
191+
``failIfEqual`` :meth:`.assertNotEqual` 3.1
192+
``failUnlessAlmostEqual`` :meth:`.assertAlmostEqual` 3.1
193+
``failIfAlmostEqual`` :meth:`.assertNotAlmostEqual` 3.1
194+
``failUnlessRaises`` :meth:`.assertRaises` 3.1
195+
``assert_`` :meth:`.assertTrue` 3.2
196+
``assertEquals`` :meth:`.assertEqual` 3.2
197+
``assertNotEquals`` :meth:`.assertNotEqual` 3.2
198+
``assertAlmostEquals`` :meth:`.assertAlmostEqual` 3.2
199+
``assertNotAlmostEquals`` :meth:`.assertNotAlmostEqual` 3.2
200+
``assertRegexpMatches`` :meth:`.assertRegex` 3.2
201+
``assertRaisesRegexp`` :meth:`.assertRaisesRegex` 3.2
202+
``assertNotRegexpMatches`` :meth:`.assertNotRegex` 3.5
203+
============================ =============================== ===============
204+
205+
You can use https://github.com/isidentical/teyit to automatically modernise
206+
your unit tests.
207+
208+
- Undocumented and broken :class:`~unittest.TestCase` method
209+
``assertDictContainsSubset`` (deprecated in Python 3.2).
210+
211+
- Undocumented :meth:`TestLoader.loadTestsFromModule
212+
<unittest.TestLoader.loadTestsFromModule>` parameter *use_load_tests*
213+
(deprecated and ignored since Python 3.2).
214+
215+
- An alias of the :class:`~unittest.TextTestResult` class:
216+
``_TextTestResult`` (deprecated in Python 3.2).
217+
218+
(Contributed by Serhiy Storchaka in :issue:`45162`.)
219+
181220
* Several names deprecated in the :mod:`configparser` way back in 3.2 have
182221
been removed per :gh:`89336`:
183222

Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,7 @@ names.
18171817
=============================== ==============================
18181818

18191819
Likewise, the ``TestCase.fail*`` methods deprecated in Python 3.1 are expected
1820-
to be removed in Python 3.3. Also see the :ref:`deprecated-aliases` section in
1821-
the :mod:`unittest` documentation.
1820+
to be removed in Python 3.3.
18221821

18231822
(Contributed by Ezio Melotti; :issue:`9424`.)
18241823

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)
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$",
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.fai 10000 lIfAlmostEqual, (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.12"""
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