|
8 | 8 | import functools |
9 | 9 | import warnings |
10 | 10 |
|
11 | | -from fnmatch import fnmatch |
| 11 | +from fnmatch import fnmatch, fnmatchcase |
12 | 12 |
|
13 | 13 | from . import case, suite, util |
14 | 14 |
|
@@ -70,6 +70,7 @@ class TestLoader(object): |
70 | 70 | """ |
71 | 71 | testMethodPrefix = 'test' |
72 | 72 | sortTestMethodsUsing = staticmethod(util.three_way_cmp) |
| 73 | + testNamePatterns = None |
73 | 74 | suiteClass = suite.TestSuite |
74 | 75 | _top_level_dir = None |
75 | 76 |
|
@@ -222,11 +223,15 @@ def loadTestsFromNames(self, names, module=None): |
222 | 223 | def getTestCaseNames(self, testCaseClass): |
223 | 224 | """Return a sorted sequence of method names found within testCaseClass |
224 | 225 | """ |
225 | | - def isTestMethod(attrname, testCaseClass=testCaseClass, |
226 | | - prefix=self.testMethodPrefix): |
227 | | - return attrname.startswith(prefix) and \ |
228 | | - callable(getattr(testCaseClass, attrname)) |
229 | | - testFnNames = list(filter(isTestMethod, dir(testCaseClass))) |
| 226 | + def shouldIncludeMethod(attrname): |
| 227 | + testFunc = getattr(testCaseClass, attrname) |
| 228 | + isTestMethod = attrname.startswith(self.testMethodPrefix) and callable(testFunc) |
| 229 | + if not isTestMethod: |
| 230 | + return False |
| 231 | + fullName = '%s.%s' % (testCaseClass.__module__, testFunc.__qualname__) |
| 232 | + return self.testNamePatterns is None or \ |
| 233 | + any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns) |
| 234 | + testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass))) |
230 | 235 | if self.sortTestMethodsUsing: |
231 | 236 | testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing)) |
232 | 237 | return testFnNames |
@@ -486,16 +491,17 @@ def _find_test_path(self, full_path, pattern, namespace=False): |
486 | 491 | defaultTestLoader = TestLoader() |
487 | 492 |
|
488 | 493 |
|
489 | | -def _makeLoader(prefix, sortUsing, suiteClass=None): |
| 494 | +def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None): |
490 | 495 | loader = TestLoader() |
491 | 496 | loader.sortTestMethodsUsing = sortUsing |
492 | 497 | loader.testMethodPrefix = prefix |
| 498 | + loader.testNamePatterns = testNamePatterns |
493 | 499 | if suiteClass: |
494 | 500 | loader.suiteClass = suiteClass |
495 | 501 | return loader |
496 | 502 |
|
497 | | -def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp): |
498 | | - return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 503 | +def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None): |
| 504 | + return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass) |
499 | 505 |
|
500 | 506 | def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, |
501 | 507 | suiteClass=suite.TestSuite): |
|
0 commit comments