mirror of https://github.com/python/cpython
bpo-35767: Fix unittest.loader to allow partials as test_functions (#11600)
This commit is contained in:
parent
f6243ac1e4
commit
fd628cf5ad
|
@ -229,7 +229,9 @@ class TestLoader(object):
|
||||||
testFunc = getattr(testCaseClass, attrname)
|
testFunc = getattr(testCaseClass, attrname)
|
||||||
if not callable(testFunc):
|
if not callable(testFunc):
|
||||||
return False
|
return False
|
||||||
fullName = '%s.%s' % (testCaseClass.__module__, testFunc.__qualname__)
|
fullName = f'%s.%s.%s' % (
|
||||||
|
testCaseClass.__module__, testCaseClass.__qualname__, attrname
|
||||||
|
)
|
||||||
return self.testNamePatterns is None or \
|
return self.testNamePatterns is None or \
|
||||||
any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
|
any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
|
||||||
testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
|
testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import functools
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -1575,5 +1576,20 @@ class Test_TestLoader(unittest.TestCase):
|
||||||
self.assertIs(loader.suiteClass, unittest.TestSuite)
|
self.assertIs(loader.suiteClass, unittest.TestSuite)
|
||||||
|
|
||||||
|
|
||||||
|
def test_partial_functions(self):
|
||||||
|
def noop(arg):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Foo(unittest.TestCase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
setattr(Foo, 'test_partial', functools.partial(noop, None))
|
||||||
|
|
||||||
|
loader = unittest.TestLoader()
|
||||||
|
|
||||||
|
test_names = ['test_partial']
|
||||||
|
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue