Skip to content
55 changes: 7 additions & 48 deletions Doc/library/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,6 @@ Test cases
:meth:`.assertRegex`.
.. versionadded:: 3.2
:meth:`.assertNotRegex`.
.. versionadded:: 3.5
The name ``assertNotRegexpMatches`` is a deprecated alias
for :meth:`.assertNotRegex`.


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


.. _deprecated-aliases:

Deprecated aliases
##################

For historical reasons, some of the :class:`TestCase` methods had one or more
aliases that are now deprecated. The following table lists the correct names
along with their deprecated aliases:

============================== ====================== =======================
Method Name Deprecated alias Deprecated alias
============================== ====================== =======================
:meth:`.assertEqual` failUnlessEqual assertEquals
:meth:`.assertNotEqual` failIfEqual assertNotEquals
:meth:`.assertTrue` failUnless assert\_
:meth:`.assertFalse` failIf
:meth:`.assertRaises` failUnlessRaises
:meth:`.assertAlmostEqual` failUnlessAlmostEqual assertAlmostEquals
:meth:`.assertNotAlmostEqual` failIfAlmostEqual assertNotAlmostEquals
:meth:`.assertRegex` assertRegexpMatches
:meth:`.assertNotRegex` assertNotRegexpMatches
:meth:`.assertRaisesRegex` assertRaisesRegexp
============================== ====================== =======================

.. deprecated:: 3.1
The fail* aliases listed in the second column have been deprecated.
.. deprecated:: 3.2
The assert* aliases listed in the third column have been deprecated.
.. deprecated:: 3.2
``assertRegexpMatches`` and ``assertRaisesRegexp`` have been renamed to
:meth:`.assertRegex` and :meth:`.assertRaisesRegex`.
.. deprecated:: 3.5
The ``assertNotRegexpMatches`` name is deprecated in favor of :meth:`.assertNotRegex`.

.. _testsuite-objects:

Grouping tests
Expand Down Expand Up @@ -1819,7 +1782,7 @@ Loading and running tests
case is created for that method instead.


.. method:: loadTestsFromModule(module, pattern=None)
.. method:: loadTestsFromModule(module, *, pattern=None)

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

.. versionchanged:: 3.5
The undocumented and unofficial *use_load_tests* default argument is
deprecated and ignored, although it is still accepted for backward
compatibility. The method also now accepts a keyword-only argument
*pattern* which is passed to ``load_tests`` as the third argument.
Support for a keyword-only argument *pattern* has been added.

.. versionchanged:: 3.12
The undocumented and unofficial *use_load_tests* parameter has been
removed.


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

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


.. data:: defaultTestLoader
Expand All @@ -2227,10 +2189,7 @@ Loading and running tests
By default this runner shows :exc:`DeprecationWarning`,
:exc:`PendingDeprecationWarning`, :exc:`ResourceWarning` and
:exc:`ImportWarning` even if they are :ref:`ignored by default
<warning-ignored>`. Deprecation warnings caused by :ref:`deprecated unittest
methods <deprecated-aliases>` are also special-cased and, when the warning
filters are ``'default'`` or ``'always'``, they will appear only once
per-module, in order to avoid too many warning messages. This behavior can
<warning-ignored>`. This behavior can
be overridden using Python's :option:`!-Wd` or :option:`!-Wa` options
(see :ref:`Warning control <using-on-warnings>`) and leaving
*warnings* to ``None``.
Expand Down
39 changes: 39 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,45 @@ although there is currently no date scheduled for their removal.
Removed
=======

* Removed many old deprecated :mod:`unittest` features:
Comment thread
serhiy-storchaka marked this conversation as resolved.

- A number of :class:`~unittest.TestCase` method aliases:

============================ =============================== ===============
Deprecated alias Method Name Deprecated in
============================ =============================== ===============
``failUnless`` :meth:`.assertTrue` 3.1
``failIf`` :meth:`.assertFalse` 3.1
``failUnlessEqual`` :meth:`.assertEqual` 3.1
``failIfEqual`` :meth:`.assertNotEqual` 3.1
``failUnlessAlmostEqual`` :meth:`.assertAlmostEqual` 3.1
``failIfAlmostEqual`` :meth:`.assertNotAlmostEqual` 3.1
``failUnlessRaises`` :meth:`.assertRaises` 3.1
``assert_`` :meth:`.assertTrue` 3.2
``assertEquals`` :meth:`.assertEqual` 3.2
``assertNotEquals`` :meth:`.assertNotEqual` 3.2
``assertAlmostEquals`` :meth:`.assertAlmostEqual` 3.2
``assertNotAlmostEquals`` :meth:`.assertNotAlmostEqual` 3.2
``assertRegexpMatches`` :meth:`.assertRegex` 3.2
``assertRaisesRegexp`` :meth:`.assertRaisesRegex` 3.2
``assertNotRegexpMatches`` :meth:`.assertNotRegex` 3.5
============================ =============================== ===============

You can use https://github.com/isidentical/teyit to automatically modernise
your unit tests.

- Undocumented and broken :class:`~unittest.TestCase` method
``assertDictContainsSubset`` (deprecated in Python 3.2).

- Undocumented :meth:`TestLoader.loadTestsFromModule
<unittest.TestLoader.loadTestsFromModule>` parameter *use_load_tests*
(deprecated and ignored since Python 3.2).

- An alias of the :class:`~unittest.TextTestResult` class:
``_TextTestResult`` (deprecated in Python 3.2).

(Contributed by Serhiy Storchaka in :issue:`45162`.)

* Several names deprecated in the :mod:`configparser` way back in 3.2 have
been removed per :gh:`89336`:

Expand Down
3 changes: 1 addition & 2 deletions Doc/whatsnew/3.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1817,8 +1817,7 @@ names.
=============================== ==============================

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

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

Expand Down
11 changes: 0 additions & 11 deletions Lib/test/test_unittest/_test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ def warnfun():
warnings.warn('rw', RuntimeWarning)

class TestWarnings(unittest.TestCase):
# unittest warnings will be printed at most once per type (max one message
# for the fail* methods, and one for the assert* methods)
def test_assert(self):
self.assertEquals(2+2, 4)
self.assertEquals(2*2, 4)
self.assertEquals(2**2, 4)

def test_fail(self):
self.failUnless(1)
self.failUnless(True)

def test_other_unittest(self):
self.assertAlmostEqual(2+2, 4)
self.assertNotAlmostEqual(4+4, 2)
Expand Down
9 changes: 0 additions & 9 deletions Lib/test/test_unittest/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,6 @@ def testAssertDictEqual(self):
r"\+ \{'key': 'value'\}$",
r"\+ \{'key': 'value'\} : oops$"])

def testAssertDictContainsSubset(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)

self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
["^Missing: 'key'$", "^oops$",
"^Missing: 'key'$",
"^Missing: 'key' : oops$"])

def testAssertMultiLineEqual(self):
self.assertMessages('assertMultiLineEqual', ("", "foo"),
[r"\+ foo$", "^oops$",
Expand Down
69 changes: 6 additions & 63 deletions Lib/test/test_unittest/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,36 +698,6 @@ def testAssertIn(self):
self.assertRaises(self.failureException, self.assertNotIn, 'cow',
animals)

def testAssertDictContainsSubset(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)

self.assertDictContainsSubset({}, {})
self.assertDictContainsSubset({}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})

with self.assertRaises(self.failureException):
self.assertDictContainsSubset({1: "one"}, {})

with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 2}, {'a': 1})

with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'c': 1}, {'a': 1})

with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})

with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})

one = ''.join(chr(i) for i in range(255))
# this used to cause a UnicodeDecodeError constructing the failure msg
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'})

def testAssertEqual(self):
equal_pairs = [
((), ()),
Expand Down Expand Up @@ -1790,45 +1760,18 @@ def testAssertNoLogsYieldsNone(self):
pass
self.assertIsNone(value)

def testDeprecatedMethodNames(self):
"""
Test that the deprecated methods raise a DeprecationWarning. See #9424.
"""
old = (
(self.failIfEqual, (3, 5)),
(self.assertNotEquals, (3, 5)),
(self.failUnlessEqual, (3, 3)),
(self.assertEquals, (3, 3)),
(self.failUnlessAlmostEqual, (2.0, 2.0)),
(self.assertAlmostEquals, (2.0, 2.0)),
(self.failIfAlmostEqual, (3.0, 5.0)),
(self.assertNotAlmostEquals, (3.0, 5.0)),
(self.failUnless, (True,)),
(self.assert_, (True,)),
(self.failUnlessRaises, (TypeError, lambda _: 3.14 + 'spam')),
(self.failIf, (False,)),
(self.assertDictContainsSubset, (dict(a=1, b=2), dict(a=1, b=2, c=3))),
(self.assertRaisesRegexp, (KeyError, 'foo', lambda: {}['foo'])),
(self.assertRegexpMatches, ('bar', 'bar')),
)
for meth, args in old:
with self.assertWarns(DeprecationWarning):
meth(*args)

# disable this test for now. When the version where the fail* methods will
# be removed is decided, re-enable it and update the version
def _testDeprecatedFailMethods(self):
"""Test that the deprecated fail* methods get removed in 3.x"""
if sys.version_info[:2] < (3, 3):
return
def testDeprecatedFailMethods(self):
"""Test that the deprecated fail* methods get removed in 3.12"""
deprecated_names = [
'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual',
'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf',
'assertDictContainsSubset',
'assertNotEquals', 'assertEquals', 'assertAlmostEquals',
'assertNotAlmostEquals', 'assert_', 'assertDictContainsSubset',
'assertRaisesRegexp', 'assertRegexpMatches'
]
for deprecated_name in deprecated_names:
with self.assertRaises(AttributeError):
getattr(self, deprecated_name) # remove these in 3.x
getattr(self, deprecated_name)

def testDeepcopy(self):
# Issue: 5660
Expand Down
Loading