mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
bpo-5846: Fix deprecations for obsolete unittest functions and add tests. (GH-28382)
This commit is contained in:
parent
773319545b
commit
b2b035a949
3 changed files with 69 additions and 30 deletions
|
@ -66,40 +66,11 @@ from .main import TestProgram, main
|
||||||
from .runner import TextTestRunner, TextTestResult
|
from .runner import TextTestRunner, TextTestResult
|
||||||
from .signals import installHandler, registerResult, removeResult, removeHandler
|
from .signals import installHandler, registerResult, removeResult, removeHandler
|
||||||
# IsolatedAsyncioTestCase will be imported lazily.
|
# IsolatedAsyncioTestCase will be imported lazily.
|
||||||
|
from .loader import makeSuite, getTestCaseNames, findTestCases
|
||||||
|
|
||||||
# deprecated
|
# deprecated
|
||||||
_TextTestResult = TextTestResult
|
_TextTestResult = TextTestResult
|
||||||
|
|
||||||
from .loader import (
|
|
||||||
makeSuite as _makeSuite,
|
|
||||||
findTestCases as _findTestCases,
|
|
||||||
getTestCaseNames as _getTestCaseNames,
|
|
||||||
)
|
|
||||||
|
|
||||||
import warnings
|
|
||||||
def makeSuite(*args, **kwargs):
|
|
||||||
warnings.warn(
|
|
||||||
"unittest.makeSuite() is deprecated and will be removed in Python 3.13. "
|
|
||||||
"Please use unittest.TestLoader.loadTestsFromTestCase() instead.",
|
|
||||||
DeprecationWarning, stacklevel=2
|
|
||||||
)
|
|
||||||
return _makeSuite(*args, **kwargs)
|
|
||||||
|
|
||||||
def getTestCaseNames(*args, **kwargs):
|
|
||||||
warnings.warn(
|
|
||||||
"unittest.getTestCaseNames() is deprecated and will be removed in Python 3.13. "
|
|
||||||
"Please use unittest.TestLoader.getTestCaseNames() instead.",
|
|
||||||
DeprecationWarning, stacklevel=2
|
|
||||||
)
|
|
||||||
return _getTestCaseNames(*args, **kwargs)
|
|
||||||
|
|
||||||
def findTestCases(*args, **kwargs):
|
|
||||||
warnings.warn(
|
|
||||||
"unittest.findTestCases() is deprecated and will be removed in Python 3.13. "
|
|
||||||
"Please use unittest.TestLoader.loadTestsFromModule() instead.",
|
|
||||||
DeprecationWarning, stacklevel=2
|
|
||||||
)
|
|
||||||
return _findTestCases(*args, **kwargs)
|
|
||||||
|
|
||||||
# There are no tests here, so don't try to run anything discovered from
|
# There are no tests here, so don't try to run anything discovered from
|
||||||
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
|
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
|
||||||
|
|
|
@ -494,6 +494,9 @@ class TestLoader(object):
|
||||||
defaultTestLoader = TestLoader()
|
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):
|
def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
|
||||||
loader = TestLoader()
|
loader = TestLoader()
|
||||||
loader.sortTestMethodsUsing = sortUsing
|
loader.sortTestMethodsUsing = sortUsing
|
||||||
|
@ -504,14 +507,32 @@ def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
|
||||||
return loader
|
return loader
|
||||||
|
|
||||||
def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
|
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)
|
return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
|
||||||
|
|
||||||
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
|
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
|
||||||
suiteClass=suite.TestSuite):
|
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(
|
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
|
||||||
testCaseClass)
|
testCaseClass)
|
||||||
|
|
||||||
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
|
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
|
||||||
suiteClass=suite.TestSuite):
|
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(\
|
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
|
||||||
module)
|
module)
|
||||||
|
|
|
@ -1591,5 +1591,52 @@ class Test_TestLoader(unittest.TestCase):
|
||||||
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
|
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
|
||||||
|
|
||||||
|
|
||||||
|
class TestObsoleteFunctions(unittest.TestCase):
|
||||||
|
class MyTestSuite(unittest.TestSuite):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MyTestCase(unittest.TestCase):
|
||||||
|
def check_1(self): pass
|
||||||
|
def check_2(self): pass
|
||||||
|
def test(self): pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
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.warnings[0].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.warnings[0].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.warnings[0].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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue