| OLD | NEW |
| 1 """Loading unittests.""" | 1 """Loading unittests.""" |
| 2 | 2 |
| 3 import os | 3 import os |
| 4 import re | 4 import re |
| 5 import sys | 5 import sys |
| 6 import traceback | 6 import traceback |
| 7 import types | 7 import types |
| 8 import functools | 8 import functools |
| 9 | 9 |
| 10 from fnmatch import fnmatch | 10 from fnmatch import fnmatch |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 class TestLoader(object): | 38 class TestLoader(object): |
| 39 """ | 39 """ |
| 40 This class is responsible for loading tests according to various criteria | 40 This class is responsible for loading tests according to various criteria |
| 41 and returning them wrapped in a TestSuite | 41 and returning them wrapped in a TestSuite |
| 42 """ | 42 """ |
| 43 testMethodPrefix = 'test' | 43 testMethodPrefix = 'test' |
| 44 sortTestMethodsUsing = staticmethod(util.three_way_cmp) | 44 sortTestMethodsUsing = staticmethod(util.three_way_cmp) |
| 45 suiteClass = suite.TestSuite | 45 suiteClass = suite.TestSuite |
| 46 _top_level_dir = None | 46 _top_level_dir = None |
| 47 | 47 |
| 48 def loadTestsFromTestCase(self, testCaseClass): | 48 def loadTestsFromTestCase(self, testCaseClass, filter=None): |
| 49 """Return a suite of all tests cases contained in testCaseClass""" | 49 """Return a suite of all tests cases contained in testCaseClass""" |
| 50 if issubclass(testCaseClass, suite.TestSuite): | 50 if issubclass(testCaseClass, suite.TestSuite): |
| 51 raise TypeError("Test cases should not be derived from TestSuite." \ | 51 raise TypeError("Test cases should not be derived from TestSuite." \ |
| 52 " Maybe you meant to derive from TestCase?") | 52 " Maybe you meant to derive from TestCase?") |
| 53 testCaseNames = self.getTestCaseNames(testCaseClass) | 53 testCaseNames = self.getTestCaseNames(testCaseClass) |
| 54 if filter: |
| 55 testCaseNames = [name for name in testCaseNames if filter(name)] |
| 54 if not testCaseNames and hasattr(testCaseClass, 'runTest'): | 56 if not testCaseNames and hasattr(testCaseClass, 'runTest'): |
| 55 testCaseNames = ['runTest'] | 57 testCaseNames = ['runTest'] |
| 56 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) | 58 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) |
| 57 return loaded_suite | 59 return loaded_suite |
| 58 | 60 |
| 59 def loadTestsFromModule(self, module, use_load_tests=True): | 61 def loadTestsFromModule(self, module, use_load_tests=True, filter=None): |
| 60 """Return a suite of all tests cases contained in the given module""" | 62 """Return a suite of all tests cases contained in the given module""" |
| 61 tests = [] | 63 tests = [] |
| 62 for name in dir(module): | 64 for name in dir(module): |
| 63 obj = getattr(module, name) | 65 obj = getattr(module, name) |
| 64 if isinstance(obj, type) and issubclass(obj, case.TestCase): | 66 if isinstance(obj, type) and issubclass(obj, case.TestCase): |
| 65 tests.append(self.loadTestsFromTestCase(obj)) | 67 tests.append(self.loadTestsFromTestCase(obj, filter=filter)) |
| 66 | 68 |
| 67 load_tests = getattr(module, 'load_tests', None) | 69 load_tests = getattr(module, 'load_tests', None) |
| 68 tests = self.suiteClass(tests) | 70 tests = self.suiteClass(tests) |
| 69 if use_load_tests and load_tests is not None: | 71 if use_load_tests and load_tests is not None: |
| 70 try: | 72 try: |
| 71 return load_tests(self, tests, None) | 73 return load_tests(self, tests, None) |
| 72 except Exception as e: | 74 except Exception as e: |
| 73 return _make_failed_load_tests(module.__name__, e, | 75 return _make_failed_load_tests(module.__name__, e, |
| 74 self.suiteClass) | 76 self.suiteClass) |
| 75 return tests | 77 return tests |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 loader.sortTestMethodsUsing = sortUsing | 306 loader.sortTestMethodsUsing = sortUsing |
| 305 loader.testMethodPrefix = prefix | 307 loader.testMethodPrefix = prefix |
| 306 if suiteClass: | 308 if suiteClass: |
| 307 loader.suiteClass = suiteClass | 309 loader.suiteClass = suiteClass |
| 308 return loader | 310 return loader |
| 309 | 311 |
| 310 def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp): | 312 def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp): |
| 311 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) | 313 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 312 | 314 |
| 313 def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, | 315 def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, |
| 314 suiteClass=suite.TestSuite): | 316 suiteClass=suite.TestSuite, filter=None): |
| 315 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase( | 317 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase( |
| 316 testCaseClass) | 318 testCaseClass, filter=filter) |
| 317 | 319 |
| 318 def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp, | 320 def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp, |
| 319 suiteClass=suite.TestSuite): | 321 suiteClass=suite.TestSuite, filter=None): |
| 320 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\ | 322 loader = _makeLoader(prefix, sortUsing, suiteClass) |
| 321 module) | 323 return loader.loadTestsFromModule(module, filter=filter) |
| OLD | NEW |