Skip to content

gh-84867: Do not load tests from TestCase and FunctionTestCase #100497

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 5 commits into from
Sep 12, 2023
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
29 changes: 29 additions & 0 deletions Lib/test/test_unittest/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def runTest(self):
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

Expand All @@ -103,6 +119,19 @@ def test(self):
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)?
Expand Down
22 changes: 17 additions & 5 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ def loadTestsFromTestCase(self, testCaseClass):
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

Expand All @@ -95,7 +99,11 @@ def loadTestsFromModule(self, module, *, pattern=None):
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)
Expand Down Expand Up @@ -164,7 +172,11 @@ def loadTestsFromName(self, name, module=None):

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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`unittest.TestLoader` no longer loads test cases from exact
:class:`unittest.TestCase` and :class:`unittest.FunctionTestCase` classes.