summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/unittest/loader.py5
-rw-r--r--Lib/unittest/test/test_loader.py23
2 files changed, 26 insertions, 2 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
index eb03b4a..d936a96 100644
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -224,9 +224,10 @@ class TestLoader(object):
"""Return a sorted sequence of method names found within testCaseClass
"""
def shouldIncludeMethod(attrname):
+ if not attrname.startswith(self.testMethodPrefix):
+ return False
testFunc = getattr(testCaseClass, attrname)
- isTestMethod = attrname.startswith(self.testMethodPrefix) and callable(testFunc)
- if not isTestMethod:
+ if not callable(testFunc):
return False
fullName = '%s.%s' % (testCaseClass.__module__, testFunc.__qualname__)
return self.testNamePatterns is None or \
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
index 15b0186..bfd7229 100644
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -1253,6 +1253,29 @@ class Test_TestLoader(unittest.TestCase):
loader.testNamePatterns = ['*my*']
self.assertEqual(loader.getTestCaseNames(MyTest), [])
+ # "Return a sorted sequence of method names found within testCaseClass"
+ #
+ # If TestLoader.testNamePatterns is set, only tests that match one of these
+ # patterns should be included.
+ #
+ # For backwards compatibility reasons (see bpo-32071), the check may only
+ # touch a TestCase's attribute if it starts with the test method prefix.
+ def test_getTestCaseNames__testNamePatterns__attribute_access_regression(self):
+ class Trap:
+ def __get__(*ignored):
+ self.fail('Non-test attribute accessed')
+
+ class MyTest(unittest.TestCase):
+ def test_1(self): pass
+ foobar = Trap()
+
+ loader = unittest.TestLoader()
+ self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])
+
+ loader = unittest.TestLoader()
+ loader.testNamePatterns = []
+ self.assertEqual(loader.getTestCaseNames(MyTest), [])
+
################################################################
### /Tests for TestLoader.getTestCaseNames()