diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-09-12 13:33:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-12 13:33:30 (GMT) |
commit | 66d1d7eb067d445f1ade151f4a6db3864dd9109f (patch) | |
tree | abce6d84d57060e7eb8ddd438063aff4d5d48ab2 /Lib/test/test_unittest | |
parent | d13f782a181d579fc3c23ea6059ff352ec9fab93 (diff) | |
download | cpython-66d1d7eb067d445f1ade151f4a6db3864dd9109f.zip cpython-66d1d7eb067d445f1ade151f4a6db3864dd9109f.tar.gz cpython-66d1d7eb067d445f1ade151f4a6db3864dd9109f.tar.bz2 |
gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497)
Diffstat (limited to 'Lib/test/test_unittest')
-rw-r--r-- | Lib/test/test_unittest/test_loader.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_unittest/test_loader.py b/Lib/test/test_unittest/test_loader.py index f32450c..83dd25c 100644 --- a/Lib/test/test_unittest/test_loader.py +++ b/Lib/test/test_unittest/test_loader.py @@ -82,6 +82,22 @@ class Test_TestLoader(unittest.TestCase): self.assertIsInstance(suite, loader.suiteClass) self.assertEqual(list(suite), [Foo('runTest')]) + # "Do not load any tests from `TestCase` class itself." + def test_loadTestsFromTestCase__from_TestCase(self): + loader = unittest.TestLoader() + + suite = loader.loadTestsFromTestCase(unittest.TestCase) + self.assertIsInstance(suite, loader.suiteClass) + self.assertEqual(list(suite), []) + + # "Do not load any tests from `FunctionTestCase` class." + def test_loadTestsFromTestCase__from_FunctionTestCase(self): + loader = unittest.TestLoader() + + suite = loader.loadTestsFromTestCase(unittest.FunctionTestCase) + self.assertIsInstance(suite, loader.suiteClass) + self.assertEqual(list(suite), []) + ################################################################ ### /Tests for TestLoader.loadTestsFromTestCase @@ -103,6 +119,19 @@ class Test_TestLoader(unittest.TestCase): expected = [loader.suiteClass([MyTestCase('test')])] self.assertEqual(list(suite), expected) + # "This test ensures that internal `TestCase` subclasses are not loaded" + def test_loadTestsFromModule__TestCase_subclass_internals(self): + # See https://github.com/python/cpython/issues/84867 + m = types.ModuleType('m') + # Simulate imported names: + m.TestCase = unittest.TestCase + m.FunctionTestCase = unittest.FunctionTestCase + + loader = unittest.TestLoader() + suite = loader.loadTestsFromModule(m) + self.assertIsInstance(suite, loader.suiteClass) + self.assertEqual(list(suite), []) + # "This method searches `module` for classes derived from TestCase" # # What happens if no tests are found (no TestCase instances)? |