Skip to content
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

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

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed Sep 11, 2023
commit 22bdd120600faa89a559400deab9a51ff36da03e
29 changes: 1 addition & 28 deletions Lib/test/test_unittest/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,7 @@ def test_loadTestsFromModule__TestCase_subclass_internals(self):
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)

expected = []
self.assertEqual(list(suite), expected)

# "This test ensures that subclasses of internal type are still loaded."
def test_loadTestsFromModule__TestCase_subclass_custom(self):
class MyBaseTestCase(unittest.TestCase):
# Base class for some 3rd-party library that should not be loaded.
@classmethod
def _shouldBeLoaded(cls):
if not super()._shouldBeLoaded():
return False
return cls != MyBaseTestCase

class MyTestCase(MyBaseTestCase):
def test_my(self):
pass

m = types.ModuleType('m')
m.MyBaseTestCase = MyBaseTestCase
m.MyTestCase = MyTestCase

loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)

expected = [loader.suiteClass([MyTestCase('test_my')])]
self.assertEqual(list(suite), expected)
self.assertEqual(list(suite), [])

# "This method searches `module` for classes derived from TestCase"
#
Expand Down
10 changes: 0 additions & 10 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,6 @@ def enterClassContext(cls, cm):
"""Same as enterContext, but class-wide."""
return _enter_context(cm, cls.addClassCleanup)

@classmethod
def _shouldBeLoaded(cls):
"""Should the subclass of `TestCase` be loaded?

We skip known unittest subclasses."""
return (
issubclass(cls, TestCase)
and cls not in (TestCase, FunctionTestCase)
)

def setUp(self):
"Hook method for setting up the test fixture before exercising it."
pass
Expand Down
6 changes: 3 additions & 3 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def loadTestsFromTestCase(self, testCaseClass):
"TestCase?")
if (
issubclass(testCaseClass, case.TestCase)
and not testCaseClass._shouldBeLoaded()
and cls in (case.TestCase, case.FunctionTestCase)
):
# We don't load any tests from base types that should not be loaded.
testCaseNames = []
Expand All @@ -106,7 +106,7 @@ def loadTestsFromModule(self, module, *, pattern=None):
if (
isinstance(obj, type)
and issubclass(obj, case.TestCase)
and obj._shouldBeLoaded()
and cls not in (case.TestCase, case.FunctionTestCase)
):
tests.append(self.loadTestsFromTestCase(obj))

Expand Down Expand Up @@ -179,7 +179,7 @@ def loadTestsFromName(self, name, module=None):
elif (
isinstance(obj, type)
and issubclass(obj, case.TestCase)
and obj._shouldBeLoaded()
and cls not in (case.TestCase, case.FunctionTestCase)
):
return self.loadTestsFromTestCase(obj)
elif (isinstance(obj, types.FunctionType) and
Expand Down
Loading