diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-09-12 14:05:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-12 14:05:58 (GMT) |
commit | b4c1cf93c782bb95980420754d2855a903f5aad1 (patch) | |
tree | aea29c0af67c364413903d7b25e8982551f9c8d7 /Lib/unittest/loader.py | |
parent | 1e8696133c6c83323e448238924fc94d462e36f0 (diff) | |
download | cpython-b4c1cf93c782bb95980420754d2855a903f5aad1.zip cpython-b4c1cf93c782bb95980420754d2855a903f5aad1.tar.gz cpython-b4c1cf93c782bb95980420754d2855a903f5aad1.tar.bz2 |
[3.11] gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497) (GH-109328)
(cherry picked from commit 66d1d7eb067d445f1ade151f4a6db3864dd9109f)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib/unittest/loader.py')
-rw-r--r-- | Lib/unittest/loader.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index 7e6ce2f..f4e3d6e 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -87,9 +87,13 @@ class TestLoader(object): raise TypeError("Test cases should not be derived from " "TestSuite. Maybe you meant to derive from " "TestCase?") - testCaseNames = self.getTestCaseNames(testCaseClass) - if not testCaseNames and hasattr(testCaseClass, 'runTest'): - testCaseNames = ['runTest'] + if testCaseClass in (case.TestCase, case.FunctionTestCase): + # We don't load any tests from base types that should not be loaded. + testCaseNames = [] + else: + testCaseNames = self.getTestCaseNames(testCaseClass) + if not testCaseNames and hasattr(testCaseClass, 'runTest'): + testCaseNames = ['runTest'] loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) return loaded_suite @@ -120,7 +124,11 @@ class TestLoader(object): tests = [] for name in dir(module): obj = getattr(module, name) - if isinstance(obj, type) and issubclass(obj, case.TestCase): + if ( + isinstance(obj, type) + and issubclass(obj, case.TestCase) + and obj not in (case.TestCase, case.FunctionTestCase) + ): tests.append(self.loadTestsFromTestCase(obj)) load_tests = getattr(module, 'load_tests', None) @@ -189,7 +197,11 @@ class TestLoader(object): if isinstance(obj, types.ModuleType): return self.loadTestsFromModule(obj) - elif isinstance(obj, type) and issubclass(obj, case.TestCase): + elif ( + isinstance(obj, type) + and issubclass(obj, case.TestCase) + and obj not in (case.TestCase, case.FunctionTestCase) + ): return self.loadTestsFromTestCase(obj) elif (isinstance(obj, types.FunctionType) and isinstance(parent, type) and |