Skip to content

bpo-5846: Deprecate obsolete methods in unittest #28299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,21 @@ Deprecated
:class:`~unittest.IsolatedAsyncioTestCase` test methods (other than the
default ``None`` value), is now deprecated.

* Deprecated the following :mod:`unittest` functions, scheduled for removal in
Python 3.13:

* :func:`unittest.findTestCases`
* :func:`unittest.makeSuite`
* :func:`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`.)


Removed
=======
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +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"}

support.check__all__(self,
unittest,
("unittest.result", "unittest.case",
Expand Down
35 changes: 33 additions & 2 deletions Lib/unittest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,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
Expand All @@ -70,6 +70,37 @@ def testMultiply(self):
# deprecated
_TextTestResult = TextTestResult

from .loader import (
makeSuite as _makeSuite,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add deprecations when they are defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, it is the "shortcuts" that are deprecated, not the methods themselves. Should I deprecate the undocumented unittest.TestLoader.makeSuite and unittest.TestLoader.findTestCases as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no unittest.TestLoader.makeSuite and unittest.TestLoader.findTestCases.

Copy link
Contributor Author

@erlend-aasland erlend-aasland Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant unittest.loader, not unittest.TestLoader:

>>> import unittest
>>> unittest.loader.makeSuite
<function makeSuite at 0x10be0e2a0>
>>> unittest.loader.findTestCases
<function findTestCases at 0x10be0e340>
>>> 

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.
Expand Down
14 changes: 14 additions & 0 deletions Misc/NEWS.d/next/Library/2020-05-25-23-58-29.bpo-5846.O9BIfm.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Deprecated the following :mod:`unittest` functions, scheduled for removal in
Python 3.13:

* :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 Erlend E. Aasland.