gh-104835: Remove unittest's deprecated getTestCaseNames, makeSuite, findTestCases (#104836)

This commit is contained in:
Hugo van Kemenade 2023-05-25 00:16:43 +03:00 committed by GitHub
parent ded5f1f287
commit b1cb30ec86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 88 deletions

View File

@ -118,6 +118,20 @@ Removed
* Remove support for using :class:`pathlib.Path` objects as context managers.
This functionality was deprecated and made a no-op in Python 3.9.
* Removed the following :mod:`unittest` functions, deprecated in Python 3.11:
* :func:`!unittest.findTestCases`
* :func:`!unittest.makeSuite`
* :func:`!unittest.getTestCaseNames`
Use :class:`~unittest.TestLoader` methods instead:
* :meth:`unittest.TestLoader.loadTestsFromModule`
* :meth:`unittest.TestLoader.loadTestsFromTestCase`
* :meth:`unittest.TestLoader.getTestCaseNames`
(Contributed by Hugo van Kemenade in :gh:`104835`.)
* :pep:`594`: Remove the :mod:`!cgi`` and :mod:`!cgitb` modules,
deprecated in Python 3.11.

View File

@ -430,10 +430,7 @@ class TestSupport(unittest.TestCase):
extra = {
'TextTestResult',
'findTestCases',
'getTestCaseNames',
'installHandler',
'makeSuite',
}
not_exported = {'load_tests', "TestProgram", "BaseTestSuite"}
support.check__all__(self,

View File

@ -1470,39 +1470,6 @@ class TestObsoleteFunctions(unittest.TestCase):
def reverse_three_way_cmp(a, b):
return unittest.util.three_way_cmp(b, a)
def test_getTestCaseNames(self):
with self.assertWarns(DeprecationWarning) as w:
tests = unittest.getTestCaseNames(self.MyTestCase,
prefix='check', sortUsing=self.reverse_three_way_cmp,
testNamePatterns=None)
self.assertEqual(w.filename, __file__)
self.assertEqual(tests, ['check_2', 'check_1'])
def test_makeSuite(self):
with self.assertWarns(DeprecationWarning) as w:
suite = unittest.makeSuite(self.MyTestCase,
prefix='check', sortUsing=self.reverse_three_way_cmp,
suiteClass=self.MyTestSuite)
self.assertEqual(w.filename, __file__)
self.assertIsInstance(suite, self.MyTestSuite)
expected = self.MyTestSuite([self.MyTestCase('check_2'),
self.MyTestCase('check_1')])
self.assertEqual(suite, expected)
def test_findTestCases(self):
m = types.ModuleType('m')
m.testcase_1 = self.MyTestCase
with self.assertWarns(DeprecationWarning) as w:
suite = unittest.findTestCases(m,
prefix='check', sortUsing=self.reverse_three_way_cmp,
suiteClass=self.MyTestSuite)
self.assertEqual(w.filename, __file__)
self.assertIsInstance(suite, self.MyTestSuite)
expected = [self.MyTestSuite([self.MyTestCase('check_2'),
self.MyTestCase('check_1')])]
self.assertEqual(list(suite), expected)
if __name__ == "__main__":
unittest.main()

View File

@ -51,10 +51,6 @@ __all__ = ['TestResult', 'TestCase', 'IsolatedAsyncioTestCase', 'TestSuite',
'registerResult', 'removeResult', 'removeHandler',
'addModuleCleanup', 'doModuleCleanups', 'enterModuleContext']
# Expose obsolete functions for backwards compatibility
# bpo-5846: Deprecated in Python 3.11, scheduled for removal in Python 3.13.
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
__unittest = True
from .result import TestResult
@ -67,7 +63,6 @@ from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
# IsolatedAsyncioTestCase will be imported lazily.
from .loader import makeSuite, getTestCaseNames, findTestCases
# Lazy import of IsolatedAsyncioTestCase from .async_case

View File

@ -437,47 +437,3 @@ class TestLoader(object):
defaultTestLoader = TestLoader()
# These functions are considered obsolete for long time.
# They will be removed in Python 3.13.
def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
loader = TestLoader()
loader.sortTestMethodsUsing = sortUsing
loader.testMethodPrefix = prefix
loader.testNamePatterns = testNamePatterns
if suiteClass:
loader.suiteClass = suiteClass
return loader
def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
import warnings
warnings.warn(
"unittest.getTestCaseNames() is deprecated and will be removed in Python 3.13. "
"Please use unittest.TestLoader.getTestCaseNames() instead.",
DeprecationWarning, stacklevel=2
)
return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
suiteClass=suite.TestSuite):
import warnings
warnings.warn(
"unittest.makeSuite() is deprecated and will be removed in Python 3.13. "
"Please use unittest.TestLoader.loadTestsFromTestCase() instead.",
DeprecationWarning, stacklevel=2
)
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
testCaseClass)
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
suiteClass=suite.TestSuite):
import warnings
warnings.warn(
"unittest.findTestCases() is deprecated and will be removed in Python 3.13. "
"Please use unittest.TestLoader.loadTestsFromModule() instead.",
DeprecationWarning, stacklevel=2
)
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
module)

View File

@ -3483,9 +3483,9 @@ Improved reprs of :mod:`threading` synchronization objects:
Deprecated the following :mod:`unittest` functions, scheduled for removal in
Python 3.13:
* :func:`~unittest.findTestCases`
* :func:`~unittest.makeSuite`
* :func:`~unittest.getTestCaseNames`
* :func:`~!unittest.findTestCases`
* :func:`~!unittest.makeSuite`
* :func:`~!unittest.getTestCaseNames`
Use :class:`~unittest.TestLoader` methods instead:

View File

@ -0,0 +1,13 @@
Removed the following :mod:`unittest` functions, deprecated in Python 3.11:
* :func:`!unittest.findTestCases`
* :func:`!unittest.makeSuite`
* :func:`!unittest.getTestCaseNames`
Use :class:`~unittest.TestLoader` methods instead:
* :meth:`unittest.TestLoader.loadTestsFromModule`
* :meth:`unittest.TestLoader.loadTestsFromTestCase`
* :meth:`unittest.TestLoader.getTestCaseNames`
Patch by Hugo van Kemenade.