| 12 | |
| 13 | |
| 14 | class MyTestLoader(unittest.TestLoader): |
| 15 | testNamePatterns = None |
| 16 | |
| 17 | def getTestCaseNames(self, testCaseClass): |
| 18 | """ |
| 19 | Customize this code to allow you to filter test methods through testNamePatterns. |
| 20 | """ |
| 21 | def shouldIncludeMethod(attrname): |
| 22 | if not attrname.startswith(self.testMethodPrefix): |
| 23 | return False |
| 24 | testFunc = getattr(testCaseClass, attrname) |
| 25 | if not callable(testFunc): |
| 26 | return False |
| 27 | return self.testNamePatterns is None or \ |
| 28 | any(fnmatchcase(attrname, pattern) for pattern in self.testNamePatterns) |
| 29 | |
| 30 | testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass))) |
| 31 | if self.sortTestMethodsUsing: |
| 32 | testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing)) |
| 33 | return testFnNames |
| 34 | |
| 35 | |
| 36 | if __name__ == '__main__': |