From c296189fdd5bdc42243a2ab4416f6157a72b9a51 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 25 May 2020 23:37:29 +0200 Subject: [PATCH 1/8] bpo-5846: Deprecate obsolete methods makeSuite, findTestCases, and getTestCaseNames in unittest Scheduled for removal in Python 3.13 --- Doc/library/unittest.rst | 2 ++ Doc/whatsnew/3.11.rst | 9 +++++++++ Lib/test/support/__init__.py | 2 +- Lib/unittest/__init__.py | 1 + Lib/unittest/loader.py | 16 ++++++++++++++++ .../2020-05-25-23-58-29.bpo-5846.O9BIfm.rst | 8 ++++++++ 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index f0fba94677a917..6d9dc58fb360f2 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -1837,6 +1837,8 @@ Loading and running tests Return a sorted sequence of method names found within *testCaseClass*; this should be a subclass of :class:`TestCase`. + .. deprecated-removed:: 3.11 3.13 + .. method:: discover(start_dir, pattern='test*.py', top_level_dir=None) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 48654040f4a894..5ff4af778ec17e 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -317,6 +317,15 @@ Deprecated It is untested and undocumented and also not used by webbrowser itself. (Contributed by Dong-hee Na in :issue:`42255`.) +* Deprecated the following :mod:`unittest` methods, scheduled for removal in + Python 3.13: + + * :meth:`~unittest.findTestCases` + * :meth:`~unittest.makeSuite` + * :meth:`~unittest.getTestCaseNames` + + (Contributed by Erlend E. Aasland in :issue:`5846`.) + Removed ======= diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index a86bfca4ce96bf..104b4941063910 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1118,7 +1118,7 @@ def run_unittest(*classes): elif isinstance(cls, valid_types): suite.addTest(cls) else: - suite.addTest(unittest.makeSuite(cls)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase(cls)) _filter_suite(suite, match_test) _run_suite(suite) diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index 348dc471f4c3d4..4c93a5407e8e3d 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -52,6 +52,7 @@ def testMultiply(self): 'addModuleCleanup'] # 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 diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index ba7105e1ad6039..8b4e3fa070234b 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -503,15 +503,31 @@ def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None): loader.suiteClass = suiteClass return loader +# bpo-5846: getTestCaseNames, makeSuite, and findTestCases are deprecated as +# of Python 3.11, scheduled for removal in Python 3.13. def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None): + warnings.warn( + "getTestCaseNames() is deprecated and will be removed in Python 3.13.", + DeprecationWarning, stacklevel=2 + ) return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass) def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, suiteClass=suite.TestSuite): + warnings.warn( + "makeSuite() is deprecated and will be removed in Python 3.13. " + "Please use 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): + warnings.warn( + "findTestCases() is deprecated and will be removed in Python 3.13. " + "Please use loadTestsFromModule() instead.", + DeprecationWarning, stacklevel=2 + ) return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\ module) diff --git a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst new file mode 100644 index 00000000000000..36e8a9790ca389 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst @@ -0,0 +1,8 @@ +Deprecated the following :mod:`unittest` methods, scheduled for removal in +Python 3.13: + +* :meth:`~unittest.findTestCases` +* :meth:`~unittest.makeSuite` +* :meth:`~unittest.getTestCaseNames` + +Patch by Erlend E. Aasland. From f44aff728965667e9823be78022ee5f7a68bc79c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 00:26:09 +0200 Subject: [PATCH 2/8] Address Serhiy's review --- Doc/whatsnew/3.11.rst | 12 +++++-- Lib/test/support/__init__.py | 3 +- Lib/unittest/__init__.py | 34 +++++++++++++++++-- Lib/unittest/loader.py | 16 --------- Lib/unittest/test/test_case.py | 2 +- .../2020-05-25-23-58-29.bpo-5846.O9BIfm.rst | 6 ++++ 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 5ff4af778ec17e..e920acb7be7f1a 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -320,9 +320,15 @@ Deprecated * Deprecated the following :mod:`unittest` methods, scheduled for removal in Python 3.13: - * :meth:`~unittest.findTestCases` - * :meth:`~unittest.makeSuite` - * :meth:`~unittest.getTestCaseNames` + * :meth:`unittest.findTestCases` + * :meth:`unittest.makeSuite` + * :meth:`unittest.getTestCaseNames` + + Use :class:`~unittest.TestLoader` method instead: + + * :meth:`unittest.TestLoader.loadTestsFromModule` + * :meth:`unittest.TestLoader.loadTestsFromTestCase` + * :meth:`unittest.TestLoader.getTestCaseNames` (Contributed by Erlend E. Aasland in :issue:`5846`.) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 104b4941063910..0fd6731146ef71 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1112,7 +1112,8 @@ def run_unittest(*classes): for cls in classes: if isinstance(cls, str): if cls in sys.modules: - suite.addTest(unittest.findTestCases(sys.modules[cls])) + loader = unittest.TestLoader() + suite.addTest(loader.loadTestsFromModule(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index 4c93a5407e8e3d..e318c635c1bccb 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -61,8 +61,7 @@ def testMultiply(self): from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip, skipIf, skipUnless, expectedFailure) from .suite import BaseTestSuite, TestSuite -from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames, - findTestCases) +from .loader import TestLoader, defaultTestLoader from .main import TestProgram, main from .runner import TextTestRunner, TextTestResult from .signals import installHandler, registerResult, removeResult, removeHandler @@ -71,6 +70,37 @@ def testMultiply(self): # deprecated _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 # introspecting the symbols (e.g. FunctionTestCase). Instead, all our # tests come from within unittest.test. diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index 8b4e3fa070234b..ba7105e1ad6039 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -503,31 +503,15 @@ def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None): loader.suiteClass = suiteClass return loader -# bpo-5846: getTestCaseNames, makeSuite, and findTestCases are deprecated as -# of Python 3.11, scheduled for removal in Python 3.13. def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None): - warnings.warn( - "getTestCaseNames() is deprecated and will be removed in Python 3.13.", - DeprecationWarning, stacklevel=2 - ) return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass) def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, suiteClass=suite.TestSuite): - warnings.warn( - "makeSuite() is deprecated and will be removed in Python 3.13. " - "Please use 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): - warnings.warn( - "findTestCases() is deprecated and will be removed in Python 3.13. " - "Please use loadTestsFromModule() instead.", - DeprecationWarning, stacklevel=2 - ) return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\ module) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 9b32da1a9f536a..b6a2f37566c0ef 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -440,7 +440,7 @@ def test_c(self): result = unittest.TestResult() result.failfast = True - suite = unittest.makeSuite(Foo) + suite = unittest.TestLoader().loadTestsFromTestCase(Foo) suite.run(result) expected = ['a1', 'a2', 'b1'] diff --git a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst index 36e8a9790ca389..a2e3c9a0418ccc 100644 --- a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst +++ b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst @@ -5,4 +5,10 @@ Python 3.13: * :meth:`~unittest.makeSuite` * :meth:`~unittest.getTestCaseNames` +Use :class:`~unittest.TestLoader` methods instead: + +* :meth:`unittest.TestLoader.loadTestsFromModule` +* :meth:`unittest.TestLoader.loadTestsFromTestCase` +* :meth:`unittest.TestLoader.getTestCaseNames` + Patch by Erlend E. Aasland. From 497e842faf9557ba140ef608ab0cddfb117d8fd1 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 00:30:50 +0200 Subject: [PATCH 3/8] Remove incorrect doc update --- Doc/library/unittest.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 6d9dc58fb360f2..f0fba94677a917 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -1837,8 +1837,6 @@ Loading and running tests Return a sorted sequence of method names found within *testCaseClass*; this should be a subclass of :class:`TestCase`. - .. deprecated-removed:: 3.11 3.13 - .. method:: discover(start_dir, pattern='test*.py', top_level_dir=None) From d6928518b62085e418fb2dd5a1e050ffb21cfaa8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 00:31:57 +0200 Subject: [PATCH 4/8] Improve NEWS/What's New wording --- Doc/whatsnew/3.11.rst | 2 +- .../NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index e920acb7be7f1a..a8e465d7d8677d 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -317,7 +317,7 @@ Deprecated It is untested and undocumented and also not used by webbrowser itself. (Contributed by Dong-hee Na in :issue:`42255`.) -* Deprecated the following :mod:`unittest` methods, scheduled for removal in +* Deprecated the following :mod:`unittest` functions, scheduled for removal in Python 3.13: * :meth:`unittest.findTestCases` diff --git a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst index a2e3c9a0418ccc..7fe5d3e4beafa8 100644 --- a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst +++ b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst @@ -1,4 +1,4 @@ -Deprecated the following :mod:`unittest` methods, scheduled for removal in +Deprecated the following :mod:`unittest` functions, scheduled for removal in Python 3.13: * :meth:`~unittest.findTestCases` From dfd6fac0a9c437e6808a98d10ada4fc49afb09a3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 00:42:47 +0200 Subject: [PATCH 5/8] Fix test_support --- Lib/test/test_support.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 11ca0c2fb2d2f3..cfa8935642f701 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -429,6 +429,7 @@ def test_check__all__(self): extra = {'TextTestResult', 'installHandler'} not_exported = {'load_tests', "TestProgram", "BaseTestSuite"} + extra = {'makeSuite', 'findTestCases', 'getTestCaseNames'} support.check__all__(self, unittest, ("unittest.result", "unittest.case", From d978b17787f50dbec6a8ac66acb104b6f4aa0d37 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 00:58:10 +0200 Subject: [PATCH 6/8] Use default shared loader --- Lib/test/support/__init__.py | 5 +++-- Lib/unittest/test/test_case.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 0fd6731146ef71..c2334774e81df6 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1112,14 +1112,15 @@ def run_unittest(*classes): for cls in classes: if isinstance(cls, str): if cls in sys.modules: - loader = unittest.TestLoader() + loader = unittest.defaultTestLoader suite.addTest(loader.loadTestsFromModule(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: - suite.addTest(unittest.TestLoader().loadTestsFromTestCase(cls)) + loader = unittest.defaultTestLoader + suite.addTest(loader.loadTestsFromTestCase(cls)) _filter_suite(suite, match_test) _run_suite(suite) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index b6a2f37566c0ef..506bcbbc842f8e 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -440,7 +440,7 @@ def test_c(self): result = unittest.TestResult() result.failfast = True - suite = unittest.TestLoader().loadTestsFromTestCase(Foo) + suite = unittest.defaultTestLoader.loadTestsFromTestCase(Foo) suite.run(result) expected = ['a1', 'a2', 'b1'] From 56aa986173f93049b867b2621de1d81514f1ed71 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 09:43:53 +0200 Subject: [PATCH 7/8] Revert "Use default shared loader" This reverts commit d978b17787f50dbec6a8ac66acb104b6f4aa0d37. --- Lib/test/support/__init__.py | 5 ++--- Lib/unittest/test/test_case.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c2334774e81df6..0fd6731146ef71 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1112,15 +1112,14 @@ def run_unittest(*classes): for cls in classes: if isinstance(cls, str): if cls in sys.modules: - loader = unittest.defaultTestLoader + loader = unittest.TestLoader() suite.addTest(loader.loadTestsFromModule(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: - loader = unittest.defaultTestLoader - suite.addTest(loader.loadTestsFromTestCase(cls)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase(cls)) _filter_suite(suite, match_test) _run_suite(suite) diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 506bcbbc842f8e..b6a2f37566c0ef 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -440,7 +440,7 @@ def test_c(self): result = unittest.TestResult() result.failfast = True - suite = unittest.defaultTestLoader.loadTestsFromTestCase(Foo) + suite = unittest.TestLoader().loadTestsFromTestCase(Foo) suite.run(result) expected = ['a1', 'a2', 'b1'] From 48741cb4d3b6619cf534a2d39afb2e9746d0507c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 13 Sep 2021 10:57:01 +0200 Subject: [PATCH 8/8] Address review --- Doc/whatsnew/3.11.rst | 6 +++--- Lib/test/test_support.py | 10 +++++++--- .../Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index a8e465d7d8677d..67a397b25d9f8d 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -320,9 +320,9 @@ Deprecated * Deprecated the following :mod:`unittest` functions, scheduled for removal in Python 3.13: - * :meth:`unittest.findTestCases` - * :meth:`unittest.makeSuite` - * :meth:`unittest.getTestCaseNames` + * :func:`unittest.findTestCases` + * :func:`unittest.makeSuite` + * :func:`unittest.getTestCaseNames` Use :class:`~unittest.TestLoader` method instead: diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index cfa8935642f701..44fe3348749e83 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -426,10 +426,14 @@ def test_check__all__(self): extra=extra, not_exported=not_exported) - extra = {'TextTestResult', 'installHandler'} + extra = { + 'TextTestResult', + 'findTestCases', + 'getTestCaseNames', + 'installHandler', + 'makeSuite', + } not_exported = {'load_tests', "TestProgram", "BaseTestSuite"} - - extra = {'makeSuite', 'findTestCases', 'getTestCaseNames'} support.check__all__(self, unittest, ("unittest.result", "unittest.case", diff --git a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst index 7fe5d3e4beafa8..556c54d0d1718b 100644 --- a/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst +++ b/Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst @@ -1,9 +1,9 @@ Deprecated the following :mod:`unittest` functions, scheduled for removal in Python 3.13: -* :meth:`~unittest.findTestCases` -* :meth:`~unittest.makeSuite` -* :meth:`~unittest.getTestCaseNames` +* :func:`~unittest.findTestCases` +* :func:`~unittest.makeSuite` +* :func:`~unittest.getTestCaseNames` Use :class:`~unittest.TestLoader` methods instead: