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 unittest | 8 import unittest |
9 | 9 |
10 from fnmatch import fnmatch | 10 from fnmatch import fnmatch |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 def _make_failed_load_tests(name, exception, suiteClass): | 47 def _make_failed_load_tests(name, exception, suiteClass): |
48 return _make_failed_test('LoadTestsFailure', name, exception, suiteClass) | 48 return _make_failed_test('LoadTestsFailure', name, exception, suiteClass) |
49 | 49 |
50 def _make_failed_test(classname, methodname, exception, suiteClass): | 50 def _make_failed_test(classname, methodname, exception, suiteClass): |
51 def testFailure(self): | 51 def testFailure(self): |
52 raise exception | 52 raise exception |
53 attrs = {methodname: testFailure} | 53 attrs = {methodname: testFailure} |
54 TestClass = type(classname, (case.TestCase,), attrs) | 54 TestClass = type(classname, (case.TestCase,), attrs) |
55 return suiteClass((TestClass(methodname),)) | 55 return suiteClass((TestClass(methodname),)) |
56 | 56 |
57 | 57 |
58 class TestLoader(unittest.TestLoader): | 58 class TestLoader(unittest.TestLoader): |
59 """ | 59 """ |
60 This class is responsible for loading tests according to various criteria | 60 This class is responsible for loading tests according to various criteria |
61 and returning them wrapped in a TestSuite | 61 and returning them wrapped in a TestSuite |
62 """ | 62 """ |
63 testMethodPrefix = 'test' | 63 testMethodPrefix = 'test' |
64 sortTestMethodsUsing = cmp | 64 sortTestMethodsUsing = cmp |
65 suiteClass = suite.TestSuite | 65 suiteClass = suite.TestSuite |
66 _top_level_dir = None | 66 _top_level_dir = None |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 name = _relpath.replace(os.path.sep, '.') | 234 name = _relpath.replace(os.path.sep, '.') |
235 return name | 235 return name |
236 | 236 |
237 def _get_module_from_name(self, name): | 237 def _get_module_from_name(self, name): |
238 __import__(name) | 238 __import__(name) |
239 return sys.modules[name] | 239 return sys.modules[name] |
240 | 240 |
241 def _match_path(self, path, full_path, pattern): | 241 def _match_path(self, path, full_path, pattern): |
242 # override this method to use alternative matching strategy | 242 # override this method to use alternative matching strategy |
243 return fnmatch(path, pattern) | 243 return fnmatch(path, pattern) |
244 | 244 |
245 def _find_tests(self, start_dir, pattern): | 245 def _find_tests(self, start_dir, pattern): |
246 """Used by discovery. Yields test suites it loads.""" | 246 """Used by discovery. Yields test suites it loads.""" |
247 paths = os.listdir(start_dir) | 247 paths = os.listdir(start_dir) |
248 | 248 |
249 for path in paths: | 249 for path in paths: |
250 full_path = os.path.join(start_dir, path) | 250 full_path = os.path.join(start_dir, path) |
251 if os.path.isfile(full_path): | 251 if os.path.isfile(full_path): |
252 if not VALID_MODULE_NAME.match(path): | 252 if not VALID_MODULE_NAME.match(path): |
253 # valid Python identifiers only | 253 # valid Python identifiers only |
254 continue | 254 continue |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): | 313 def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp): |
314 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) | 314 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
315 | 315 |
316 def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, | 316 def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, |
317 suiteClass=suite.TestSuite): | 317 suiteClass=suite.TestSuite): |
318 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(test
CaseClass) | 318 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(test
CaseClass) |
319 | 319 |
320 def findTestCases(module, prefix='test', sortUsing=cmp, | 320 def findTestCases(module, prefix='test', sortUsing=cmp, |
321 suiteClass=suite.TestSuite): | 321 suiteClass=suite.TestSuite): |
322 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module
) | 322 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module
) |
OLD | NEW |