Skip to content

Commit ff6d2cc

Browse files
author
Erlend Egeberg Aasland
authored
bpo-5846: Deprecate obsolete methods in unittest (GH-28299)
Deprecate makeSuite, findTestCases, and getTestCaseNames. Scheduled for removal in Python 3.13.
1 parent 9d76d28 commit ff6d2cc

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

Doc/whatsnew/3.11.rst

+15
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,21 @@ Deprecated
352352
:class:`~unittest.IsolatedAsyncioTestCase` test methods (other than the
353353
default ``None`` value), is now deprecated.
354354

355+
* Deprecated the following :mod:`unittest` functions, scheduled for removal in
356+
Python 3.13:
357+
358+
* :func:`unittest.findTestCases`
359+
* :func:`unittest.makeSuite`
360+
* :func:`unittest.getTestCaseNames`
361+
362+
Use :class:`~unittest.TestLoader` method instead:
363+
364+
* :meth:`unittest.TestLoader.loadTestsFromModule`
365+
* :meth:`unittest.TestLoader.loadTestsFromTestCase`
366+
* :meth:`unittest.TestLoader.getTestCaseNames`
367+
368+
(Contributed by Erlend E. Aasland in :issue:`5846`.)
369+
355370

356371
Removed
357372
=======

Lib/test/test_support.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,14 @@ def test_check__all__(self):
426426
extra=extra,
427427
not_exported=not_exported)
428428

429-
extra = {'TextTestResult', 'installHandler'}
429+
extra = {
430+
'TextTestResult',
431+
'findTestCases',
432+
'getTestCaseNames',
433+
'installHandler',
434+
'makeSuite',
435+
}
430436
not_exported = {'load_tests', "TestProgram", "BaseTestSuite"}
431-
432437
support.check__all__(self,
433438
unittest,
434439
("unittest.result", "unittest.case",

Lib/unittest/__init__.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def testMultiply(self):
5252
'addModuleCleanup']
5353

5454
# Expose obsolete functions for backwards compatibility
55+
# bpo-5846: Deprecated in Python 3.11, scheduled for removal in Python 3.13.
5556
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
5657

5758
__unittest = True
@@ -60,8 +61,7 @@ def testMultiply(self):
6061
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
6162
skipIf, skipUnless, expectedFailure)
6263
from .suite import BaseTestSuite, TestSuite
63-
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
64-
findTestCases)
64+
from .loader import TestLoader, defaultTestLoader
6565
from .main import TestProgram, main
6666
from .runner import TextTestRunner, TextTestResult
6767
from .signals import installHandler, registerResult, removeResult, removeHandler
@@ -70,6 +70,37 @@ def testMultiply(self):
7070
# deprecated
7171
_TextTestResult = TextTestResult
7272

73+
from .loader import (
74+
makeSuite as _makeSuite,
75+
findTestCases as _findTestCases,
76+
getTestCaseNames as _getTestCaseNames,
77+
)
78+
79+
import warnings
80+
def makeSuite(*args, **kwargs):
81+
warnings.warn(
82+
"unittest.makeSuite() is deprecated and will be removed in Python 3.13. "
83+
"Please use unittest.TestLoader.loadTestsFromTestCase() instead.",
84+
DeprecationWarning, stacklevel=2
85+
)
86+
return _makeSuite(*args, **kwargs)
87+
88+
def getTestCaseNames(*args, **kwargs):
89+
warnings.warn(
90+
"unittest.getTestCaseNames() is deprecated and will be removed in Python 3.13. "
91+
"Please use unittest.TestLoader.getTestCaseNames() instead.",
92+
DeprecationWarning, stacklevel=2
93+
)
94+
return _getTestCaseNames(*args, **kwargs)
95+
96+
def findTestCases(*args, **kwargs):
97+
warnings.warn(
98+
"unittest.findTestCases() is deprecated and will be removed in Python 3.13. "
99+
"Please use unittest.TestLoader.loadTestsFromModule() instead.",
100+
DeprecationWarning, stacklevel=2
101+
)
102+
return _findTestCases(*args, **kwargs)
103+
73104
# There are no tests here, so don't try to run anything discovered from
74105
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
75106
# tests come from within unittest.test.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Deprecated the following :mod:`unittest` functions, scheduled for removal in
2+
Python 3.13:
3+
4+
* :func:`~unittest.findTestCases`
5+
* :func:`~unittest.makeSuite`
6+
* :func:`~unittest.getTestCaseNames`
7+
8+
Use :class:`~unittest.TestLoader` methods instead:
9+
10+
* :meth:`unittest.TestLoader.loadTestsFromModule`
11+
* :meth:`unittest.TestLoader.loadTestsFromTestCase`
12+
* :meth:`unittest.TestLoader.getTestCaseNames`
13+
14+
Patch by Erlend E. Aasland.

0 commit comments

Comments
 (0)