Skip to content

Commit 450c3be

Browse files
bpo-5846: Fix deprecations for obsolete unittest functions and add tests.
1 parent 9fd87a5 commit 450c3be

File tree

3 files changed

+69
-30
lines changed

3 files changed

+69
-30
lines changed

Lib/unittest/__init__.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,11 @@ def testMultiply(self):
6666
from .runner import TextTestRunner, TextTestResult
6767
from .signals import installHandler, registerResult, removeResult, removeHandler
6868
# IsolatedAsyncioTestCase will be imported lazily.
69+
from .loader import makeSuite, getTestCaseNames, findTestCases
6970

7071
# deprecated
7172
_TextTestResult = TextTestResult
7273

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)
10374

10475
# There are no tests here, so don't try to run anything discovered from
10576
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our

Lib/unittest/loader.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ def _find_test_path(self, full_path, pattern, namespace=False):
494494
defaultTestLoader = TestLoader()
495495

496496

497+
# These functions are considered obsolete for long time.
498+
# They will be removed in Python 3.13.
499+
497500
def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
498501
loader = TestLoader()
499502
loader.sortTestMethodsUsing = sortUsing
@@ -504,14 +507,32 @@ def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
504507
return loader
505508

506509
def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
510+
import warnings
511+
warnings.warn(
512+
"unittest.getTestCaseNames() is deprecated and will be removed in Python 3.13. "
513+
"Please use unittest.TestLoader.getTestCaseNames() instead.",
514+
DeprecationWarning, stacklevel=2
515+
)
507516
return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
508517

509518
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
510519
suiteClass=suite.TestSuite):
520+
import warnings
521+
warnings.warn(
522+
"unittest.makeSuite() is deprecated and will be removed in Python 3.13. "
523+
"Please use unittest.TestLoader.loadTestsFromTestCase() instead.",
524+
DeprecationWarning, stacklevel=2
525+
)
511526
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
512527
testCaseClass)
513528

514529
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
515530
suiteClass=suite.TestSuite):
531+
import warnings
532+
warnings.warn(
533+
"unittest.findTestCases() is deprecated and will be removed in Python 3.13. "
534+
"Please use unittest.TestLoader.loadTestsFromModule() instead.",
535+
DeprecationWarning, stacklevel=2
536+
)
516537
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
517538
module)

Lib/unittest/test/test_loader.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,5 +1591,52 @@ class Foo(unittest.TestCase):
15911591
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
15921592

15931593

1594+
class TestObsoleteFunctions(unittest.TestCase):
1595+
class MyTestSuite(unittest.TestSuite):
1596+
pass
1597+
1598+
class MyTestCase(unittest.TestCase):
1599+
def check_1(self): pass
1600+
def check_2(self): pass
1601+
def test(self): pass
1602+
1603+
@staticmethod
1604+
def reverse_three_way_cmp(a, b):
1605+
return unittest.util.three_way_cmp(b, a)
1606+
1607+
def test_getTestCaseNames(self):
1608+
with self.assertWarns(DeprecationWarning) as w:
1609+
tests = unittest.getTestCaseNames(self.MyTestCase,
1610+
prefix='check', sortUsing=self.reverse_three_way_cmp,
1611+
testNamePatterns=None)
1612+
self.assertEqual(w.warnings[0].filename, __file__)
1613+
self.assertEqual(tests, ['check_2', 'check_1'])
1614+
1615+
def test_makeSuite(self):
1616+
with self.assertWarns(DeprecationWarning) as w:
1617+
suite = unittest.makeSuite(self.MyTestCase,
1618+
prefix='check', sortUsing=self.reverse_three_way_cmp,
1619+
suiteClass=self.MyTestSuite)
1620+
self.assertEqual(w.warnings[0].filename, __file__)
1621+
self.assertIsInstance(suite, self.MyTestSuite)
1622+
expected = self.MyTestSuite([self.MyTestCase('check_2'),
1623+
self.MyTestCase('check_1')])
1624+
self.assertEqual(suite, expected)
1625+
1626+
def test_findTestCases(self):
1627+
m = types.ModuleType('m')
1628+
m.testcase_1 = self.MyTestCase
1629+
1630+
with self.assertWarns(DeprecationWarning) as w:
1631+
suite = unittest.findTestCases(m,
1632+
prefix='check', sortUsing=self.reverse_three_way_cmp,
1633+
suiteClass=self.MyTestSuite)
1634+
self.assertEqual(w.warnings[0].filename, __file__)
1635+
self.assertIsInstance(suite, self.MyTestSuite)
1636+
expected = [self.MyTestSuite([self.MyTestCase('check_2'),
1637+
self.MyTestCase('check_1')])]
1638+
self.assertEqual(list(suite), expected)
1639+
1640+
15941641
if __name__ == "__main__":
15951642
unittest.main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy