jpayne@69
|
1 """Loading unittests."""
|
jpayne@69
|
2
|
jpayne@69
|
3 import os
|
jpayne@69
|
4 import re
|
jpayne@69
|
5 import sys
|
jpayne@69
|
6 import traceback
|
jpayne@69
|
7 import types
|
jpayne@69
|
8 import functools
|
jpayne@69
|
9 import warnings
|
jpayne@69
|
10
|
jpayne@69
|
11 from fnmatch import fnmatch, fnmatchcase
|
jpayne@69
|
12
|
jpayne@69
|
13 from . import case, suite, util
|
jpayne@69
|
14
|
jpayne@69
|
15 __unittest = True
|
jpayne@69
|
16
|
jpayne@69
|
17 # what about .pyc (etc)
|
jpayne@69
|
18 # we would need to avoid loading the same tests multiple times
|
jpayne@69
|
19 # from '.py', *and* '.pyc'
|
jpayne@69
|
20 VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
|
jpayne@69
|
21
|
jpayne@69
|
22
|
jpayne@69
|
23 class _FailedTest(case.TestCase):
|
jpayne@69
|
24 _testMethodName = None
|
jpayne@69
|
25
|
jpayne@69
|
26 def __init__(self, method_name, exception):
|
jpayne@69
|
27 self._exception = exception
|
jpayne@69
|
28 super(_FailedTest, self).__init__(method_name)
|
jpayne@69
|
29
|
jpayne@69
|
30 def __getattr__(self, name):
|
jpayne@69
|
31 if name != self._testMethodName:
|
jpayne@69
|
32 return super(_FailedTest, self).__getattr__(name)
|
jpayne@69
|
33 def testFailure():
|
jpayne@69
|
34 raise self._exception
|
jpayne@69
|
35 return testFailure
|
jpayne@69
|
36
|
jpayne@69
|
37
|
jpayne@69
|
38 def _make_failed_import_test(name, suiteClass):
|
jpayne@69
|
39 message = 'Failed to import test module: %s\n%s' % (
|
jpayne@69
|
40 name, traceback.format_exc())
|
jpayne@69
|
41 return _make_failed_test(name, ImportError(message), suiteClass, message)
|
jpayne@69
|
42
|
jpayne@69
|
43 def _make_failed_load_tests(name, exception, suiteClass):
|
jpayne@69
|
44 message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
|
jpayne@69
|
45 return _make_failed_test(
|
jpayne@69
|
46 name, exception, suiteClass, message)
|
jpayne@69
|
47
|
jpayne@69
|
48 def _make_failed_test(methodname, exception, suiteClass, message):
|
jpayne@69
|
49 test = _FailedTest(methodname, exception)
|
jpayne@69
|
50 return suiteClass((test,)), message
|
jpayne@69
|
51
|
jpayne@69
|
52 def _make_skipped_test(methodname, exception, suiteClass):
|
jpayne@69
|
53 @case.skip(str(exception))
|
jpayne@69
|
54 def testSkipped(self):
|
jpayne@69
|
55 pass
|
jpayne@69
|
56 attrs = {methodname: testSkipped}
|
jpayne@69
|
57 TestClass = type("ModuleSkipped", (case.TestCase,), attrs)
|
jpayne@69
|
58 return suiteClass((TestClass(methodname),))
|
jpayne@69
|
59
|
jpayne@69
|
60 def _jython_aware_splitext(path):
|
jpayne@69
|
61 if path.lower().endswith('$py.class'):
|
jpayne@69
|
62 return path[:-9]
|
jpayne@69
|
63 return os.path.splitext(path)[0]
|
jpayne@69
|
64
|
jpayne@69
|
65
|
jpayne@69
|
66 class TestLoader(object):
|
jpayne@69
|
67 """
|
jpayne@69
|
68 This class is responsible for loading tests according to various criteria
|
jpayne@69
|
69 and returning them wrapped in a TestSuite
|
jpayne@69
|
70 """
|
jpayne@69
|
71 testMethodPrefix = 'test'
|
jpayne@69
|
72 sortTestMethodsUsing = staticmethod(util.three_way_cmp)
|
jpayne@69
|
73 testNamePatterns = None
|
jpayne@69
|
74 suiteClass = suite.TestSuite
|
jpayne@69
|
75 _top_level_dir = None
|
jpayne@69
|
76
|
jpayne@69
|
77 def __init__(self):
|
jpayne@69
|
78 super(TestLoader, self).__init__()
|
jpayne@69
|
79 self.errors = []
|
jpayne@69
|
80 # Tracks packages which we have called into via load_tests, to
|
jpayne@69
|
81 # avoid infinite re-entrancy.
|
jpayne@69
|
82 self._loading_packages = set()
|
jpayne@69
|
83
|
jpayne@69
|
84 def loadTestsFromTestCase(self, testCaseClass):
|
jpayne@69
|
85 """Return a suite of all test cases contained in testCaseClass"""
|
jpayne@69
|
86 if issubclass(testCaseClass, suite.TestSuite):
|
jpayne@69
|
87 raise TypeError("Test cases should not be derived from "
|
jpayne@69
|
88 "TestSuite. Maybe you meant to derive from "
|
jpayne@69
|
89 "TestCase?")
|
jpayne@69
|
90 testCaseNames = self.getTestCaseNames(testCaseClass)
|
jpayne@69
|
91 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
|
jpayne@69
|
92 testCaseNames = ['runTest']
|
jpayne@69
|
93 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
|
jpayne@69
|
94 return loaded_suite
|
jpayne@69
|
95
|
jpayne@69
|
96 # XXX After Python 3.5, remove backward compatibility hacks for
|
jpayne@69
|
97 # use_load_tests deprecation via *args and **kws. See issue 16662.
|
jpayne@69
|
98 def loadTestsFromModule(self, module, *args, pattern=None, **kws):
|
jpayne@69
|
99 """Return a suite of all test cases contained in the given module"""
|
jpayne@69
|
100 # This method used to take an undocumented and unofficial
|
jpayne@69
|
101 # use_load_tests argument. For backward compatibility, we still
|
jpayne@69
|
102 # accept the argument (which can also be the first position) but we
|
jpayne@69
|
103 # ignore it and issue a deprecation warning if it's present.
|
jpayne@69
|
104 if len(args) > 0 or 'use_load_tests' in kws:
|
jpayne@69
|
105 warnings.warn('use_load_tests is deprecated and ignored',
|
jpayne@69
|
106 DeprecationWarning)
|
jpayne@69
|
107 kws.pop('use_load_tests', None)
|
jpayne@69
|
108 if len(args) > 1:
|
jpayne@69
|
109 # Complain about the number of arguments, but don't forget the
|
jpayne@69
|
110 # required `module` argument.
|
jpayne@69
|
111 complaint = len(args) + 1
|
jpayne@69
|
112 raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
|
jpayne@69
|
113 if len(kws) != 0:
|
jpayne@69
|
114 # Since the keyword arguments are unsorted (see PEP 468), just
|
jpayne@69
|
115 # pick the alphabetically sorted first argument to complain about,
|
jpayne@69
|
116 # if multiple were given. At least the error message will be
|
jpayne@69
|
117 # predictable.
|
jpayne@69
|
118 complaint = sorted(kws)[0]
|
jpayne@69
|
119 raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
|
jpayne@69
|
120 tests = []
|
jpayne@69
|
121 for name in dir(module):
|
jpayne@69
|
122 obj = getattr(module, name)
|
jpayne@69
|
123 if isinstance(obj, type) and issubclass(obj, case.TestCase):
|
jpayne@69
|
124 tests.append(self.loadTestsFromTestCase(obj))
|
jpayne@69
|
125
|
jpayne@69
|
126 load_tests = getattr(module, 'load_tests', None)
|
jpayne@69
|
127 tests = self.suiteClass(tests)
|
jpayne@69
|
128 if load_tests is not None:
|
jpayne@69
|
129 try:
|
jpayne@69
|
130 return load_tests(self, tests, pattern)
|
jpayne@69
|
131 except Exception as e:
|
jpayne@69
|
132 error_case, error_message = _make_failed_load_tests(
|
jpayne@69
|
133 module.__name__, e, self.suiteClass)
|
jpayne@69
|
134 self.errors.append(error_message)
|
jpayne@69
|
135 return error_case
|
jpayne@69
|
136 return tests
|
jpayne@69
|
137
|
jpayne@69
|
138 def loadTestsFromName(self, name, module=None):
|
jpayne@69
|
139 """Return a suite of all test cases given a string specifier.
|
jpayne@69
|
140
|
jpayne@69
|
141 The name may resolve either to a module, a test case class, a
|
jpayne@69
|
142 test method within a test case class, or a callable object which
|
jpayne@69
|
143 returns a TestCase or TestSuite instance.
|
jpayne@69
|
144
|
jpayne@69
|
145 The method optionally resolves the names relative to a given module.
|
jpayne@69
|
146 """
|
jpayne@69
|
147 parts = name.split('.')
|
jpayne@69
|
148 error_case, error_message = None, None
|
jpayne@69
|
149 if module is None:
|
jpayne@69
|
150 parts_copy = parts[:]
|
jpayne@69
|
151 while parts_copy:
|
jpayne@69
|
152 try:
|
jpayne@69
|
153 module_name = '.'.join(parts_copy)
|
jpayne@69
|
154 module = __import__(module_name)
|
jpayne@69
|
155 break
|
jpayne@69
|
156 except ImportError:
|
jpayne@69
|
157 next_attribute = parts_copy.pop()
|
jpayne@69
|
158 # Last error so we can give it to the user if needed.
|
jpayne@69
|
159 error_case, error_message = _make_failed_import_test(
|
jpayne@69
|
160 next_attribute, self.suiteClass)
|
jpayne@69
|
161 if not parts_copy:
|
jpayne@69
|
162 # Even the top level import failed: report that error.
|
jpayne@69
|
163 self.errors.append(error_message)
|
jpayne@69
|
164 return error_case
|
jpayne@69
|
165 parts = parts[1:]
|
jpayne@69
|
166 obj = module
|
jpayne@69
|
167 for part in parts:
|
jpayne@69
|
168 try:
|
jpayne@69
|
169 parent, obj = obj, getattr(obj, part)
|
jpayne@69
|
170 except AttributeError as e:
|
jpayne@69
|
171 # We can't traverse some part of the name.
|
jpayne@69
|
172 if (getattr(obj, '__path__', None) is not None
|
jpayne@69
|
173 and error_case is not None):
|
jpayne@69
|
174 # This is a package (no __path__ per importlib docs), and we
|
jpayne@69
|
175 # encountered an error importing something. We cannot tell
|
jpayne@69
|
176 # the difference between package.WrongNameTestClass and
|
jpayne@69
|
177 # package.wrong_module_name so we just report the
|
jpayne@69
|
178 # ImportError - it is more informative.
|
jpayne@69
|
179 self.errors.append(error_message)
|
jpayne@69
|
180 return error_case
|
jpayne@69
|
181 else:
|
jpayne@69
|
182 # Otherwise, we signal that an AttributeError has occurred.
|
jpayne@69
|
183 error_case, error_message = _make_failed_test(
|
jpayne@69
|
184 part, e, self.suiteClass,
|
jpayne@69
|
185 'Failed to access attribute:\n%s' % (
|
jpayne@69
|
186 traceback.format_exc(),))
|
jpayne@69
|
187 self.errors.append(error_message)
|
jpayne@69
|
188 return error_case
|
jpayne@69
|
189
|
jpayne@69
|
190 if isinstance(obj, types.ModuleType):
|
jpayne@69
|
191 return self.loadTestsFromModule(obj)
|
jpayne@69
|
192 elif isinstance(obj, type) and issubclass(obj, case.TestCase):
|
jpayne@69
|
193 return self.loadTestsFromTestCase(obj)
|
jpayne@69
|
194 elif (isinstance(obj, types.FunctionType) and
|
jpayne@69
|
195 isinstance(parent, type) and
|
jpayne@69
|
196 issubclass(parent, case.TestCase)):
|
jpayne@69
|
197 name = parts[-1]
|
jpayne@69
|
198 inst = parent(name)
|
jpayne@69
|
199 # static methods follow a different path
|
jpayne@69
|
200 if not isinstance(getattr(inst, name), types.FunctionType):
|
jpayne@69
|
201 return self.suiteClass([inst])
|
jpayne@69
|
202 elif isinstance(obj, suite.TestSuite):
|
jpayne@69
|
203 return obj
|
jpayne@69
|
204 if callable(obj):
|
jpayne@69
|
205 test = obj()
|
jpayne@69
|
206 if isinstance(test, suite.TestSuite):
|
jpayne@69
|
207 return test
|
jpayne@69
|
208 elif isinstance(test, case.TestCase):
|
jpayne@69
|
209 return self.suiteClass([test])
|
jpayne@69
|
210 else:
|
jpayne@69
|
211 raise TypeError("calling %s returned %s, not a test" %
|
jpayne@69
|
212 (obj, test))
|
jpayne@69
|
213 else:
|
jpayne@69
|
214 raise TypeError("don't know how to make test from: %s" % obj)
|
jpayne@69
|
215
|
jpayne@69
|
216 def loadTestsFromNames(self, names, module=None):
|
jpayne@69
|
217 """Return a suite of all test cases found using the given sequence
|
jpayne@69
|
218 of string specifiers. See 'loadTestsFromName()'.
|
jpayne@69
|
219 """
|
jpayne@69
|
220 suites = [self.loadTestsFromName(name, module) for name in names]
|
jpayne@69
|
221 return self.suiteClass(suites)
|
jpayne@69
|
222
|
jpayne@69
|
223 def getTestCaseNames(self, testCaseClass):
|
jpayne@69
|
224 """Return a sorted sequence of method names found within testCaseClass
|
jpayne@69
|
225 """
|
jpayne@69
|
226 def shouldIncludeMethod(attrname):
|
jpayne@69
|
227 if not attrname.startswith(self.testMethodPrefix):
|
jpayne@69
|
228 return False
|
jpayne@69
|
229 testFunc = getattr(testCaseClass, attrname)
|
jpayne@69
|
230 if not callable(testFunc):
|
jpayne@69
|
231 return False
|
jpayne@69
|
232 fullName = f'%s.%s.%s' % (
|
jpayne@69
|
233 testCaseClass.__module__, testCaseClass.__qualname__, attrname
|
jpayne@69
|
234 )
|
jpayne@69
|
235 return self.testNamePatterns is None or \
|
jpayne@69
|
236 any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
|
jpayne@69
|
237 testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
|
jpayne@69
|
238 if self.sortTestMethodsUsing:
|
jpayne@69
|
239 testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
|
jpayne@69
|
240 return testFnNames
|
jpayne@69
|
241
|
jpayne@69
|
242 def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
|
jpayne@69
|
243 """Find and return all test modules from the specified start
|
jpayne@69
|
244 directory, recursing into subdirectories to find them and return all
|
jpayne@69
|
245 tests found within them. Only test files that match the pattern will
|
jpayne@69
|
246 be loaded. (Using shell style pattern matching.)
|
jpayne@69
|
247
|
jpayne@69
|
248 All test modules must be importable from the top level of the project.
|
jpayne@69
|
249 If the start directory is not the top level directory then the top
|
jpayne@69
|
250 level directory must be specified separately.
|
jpayne@69
|
251
|
jpayne@69
|
252 If a test package name (directory with '__init__.py') matches the
|
jpayne@69
|
253 pattern then the package will be checked for a 'load_tests' function. If
|
jpayne@69
|
254 this exists then it will be called with (loader, tests, pattern) unless
|
jpayne@69
|
255 the package has already had load_tests called from the same discovery
|
jpayne@69
|
256 invocation, in which case the package module object is not scanned for
|
jpayne@69
|
257 tests - this ensures that when a package uses discover to further
|
jpayne@69
|
258 discover child tests that infinite recursion does not happen.
|
jpayne@69
|
259
|
jpayne@69
|
260 If load_tests exists then discovery does *not* recurse into the package,
|
jpayne@69
|
261 load_tests is responsible for loading all tests in the package.
|
jpayne@69
|
262
|
jpayne@69
|
263 The pattern is deliberately not stored as a loader attribute so that
|
jpayne@69
|
264 packages can continue discovery themselves. top_level_dir is stored so
|
jpayne@69
|
265 load_tests does not need to pass this argument in to loader.discover().
|
jpayne@69
|
266
|
jpayne@69
|
267 Paths are sorted before being imported to ensure reproducible execution
|
jpayne@69
|
268 order even on filesystems with non-alphabetical ordering like ext3/4.
|
jpayne@69
|
269 """
|
jpayne@69
|
270 set_implicit_top = False
|
jpayne@69
|
271 if top_level_dir is None and self._top_level_dir is not None:
|
jpayne@69
|
272 # make top_level_dir optional if called from load_tests in a package
|
jpayne@69
|
273 top_level_dir = self._top_level_dir
|
jpayne@69
|
274 elif top_level_dir is None:
|
jpayne@69
|
275 set_implicit_top = True
|
jpayne@69
|
276 top_level_dir = start_dir
|
jpayne@69
|
277
|
jpayne@69
|
278 top_level_dir = os.path.abspath(top_level_dir)
|
jpayne@69
|
279
|
jpayne@69
|
280 if not top_level_dir in sys.path:
|
jpayne@69
|
281 # all test modules must be importable from the top level directory
|
jpayne@69
|
282 # should we *unconditionally* put the start directory in first
|
jpayne@69
|
283 # in sys.path to minimise likelihood of conflicts between installed
|
jpayne@69
|
284 # modules and development versions?
|
jpayne@69
|
285 sys.path.insert(0, top_level_dir)
|
jpayne@69
|
286 self._top_level_dir = top_level_dir
|
jpayne@69
|
287
|
jpayne@69
|
288 is_not_importable = False
|
jpayne@69
|
289 is_namespace = False
|
jpayne@69
|
290 tests = []
|
jpayne@69
|
291 if os.path.isdir(os.path.abspath(start_dir)):
|
jpayne@69
|
292 start_dir = os.path.abspath(start_dir)
|
jpayne@69
|
293 if start_dir != top_level_dir:
|
jpayne@69
|
294 is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
|
jpayne@69
|
295 else:
|
jpayne@69
|
296 # support for discovery from dotted module names
|
jpayne@69
|
297 try:
|
jpayne@69
|
298 __import__(start_dir)
|
jpayne@69
|
299 except ImportError:
|
jpayne@69
|
300 is_not_importable = True
|
jpayne@69
|
301 else:
|
jpayne@69
|
302 the_module = sys.modules[start_dir]
|
jpayne@69
|
303 top_part = start_dir.split('.')[0]
|
jpayne@69
|
304 try:
|
jpayne@69
|
305 start_dir = os.path.abspath(
|
jpayne@69
|
306 os.path.dirname((the_module.__file__)))
|
jpayne@69
|
307 except AttributeError:
|
jpayne@69
|
308 # look for namespace packages
|
jpayne@69
|
309 try:
|
jpayne@69
|
310 spec = the_module.__spec__
|
jpayne@69
|
311 except AttributeError:
|
jpayne@69
|
312 spec = None
|
jpayne@69
|
313
|
jpayne@69
|
314 if spec and spec.loader is None:
|
jpayne@69
|
315 if spec.submodule_search_locations is not None:
|
jpayne@69
|
316 is_namespace = True
|
jpayne@69
|
317
|
jpayne@69
|
318 for path in the_module.__path__:
|
jpayne@69
|
319 if (not set_implicit_top and
|
jpayne@69
|
320 not path.startswith(top_level_dir)):
|
jpayne@69
|
321 continue
|
jpayne@69
|
322 self._top_level_dir = \
|
jpayne@69
|
323 (path.split(the_module.__name__
|
jpayne@69
|
324 .replace(".", os.path.sep))[0])
|
jpayne@69
|
325 tests.extend(self._find_tests(path,
|
jpayne@69
|
326 pattern,
|
jpayne@69
|
327 namespace=True))
|
jpayne@69
|
328 elif the_module.__name__ in sys.builtin_module_names:
|
jpayne@69
|
329 # builtin module
|
jpayne@69
|
330 raise TypeError('Can not use builtin modules '
|
jpayne@69
|
331 'as dotted module names') from None
|
jpayne@69
|
332 else:
|
jpayne@69
|
333 raise TypeError(
|
jpayne@69
|
334 'don\'t know how to discover from {!r}'
|
jpayne@69
|
335 .format(the_module)) from None
|
jpayne@69
|
336
|
jpayne@69
|
337 if set_implicit_top:
|
jpayne@69
|
338 if not is_namespace:
|
jpayne@69
|
339 self._top_level_dir = \
|
jpayne@69
|
340 self._get_directory_containing_module(top_part)
|
jpayne@69
|
341 sys.path.remove(top_level_dir)
|
jpayne@69
|
342 else:
|
jpayne@69
|
343 sys.path.remove(top_level_dir)
|
jpayne@69
|
344
|
jpayne@69
|
345 if is_not_importable:
|
jpayne@69
|
346 raise ImportError('Start directory is not importable: %r' % start_dir)
|
jpayne@69
|
347
|
jpayne@69
|
348 if not is_namespace:
|
jpayne@69
|
349 tests = list(self._find_tests(start_dir, pattern))
|
jpayne@69
|
350 return self.suiteClass(tests)
|
jpayne@69
|
351
|
jpayne@69
|
352 def _get_directory_containing_module(self, module_name):
|
jpayne@69
|
353 module = sys.modules[module_name]
|
jpayne@69
|
354 full_path = os.path.abspath(module.__file__)
|
jpayne@69
|
355
|
jpayne@69
|
356 if os.path.basename(full_path).lower().startswith('__init__.py'):
|
jpayne@69
|
357 return os.path.dirname(os.path.dirname(full_path))
|
jpayne@69
|
358 else:
|
jpayne@69
|
359 # here we have been given a module rather than a package - so
|
jpayne@69
|
360 # all we can do is search the *same* directory the module is in
|
jpayne@69
|
361 # should an exception be raised instead
|
jpayne@69
|
362 return os.path.dirname(full_path)
|
jpayne@69
|
363
|
jpayne@69
|
364 def _get_name_from_path(self, path):
|
jpayne@69
|
365 if path == self._top_level_dir:
|
jpayne@69
|
366 return '.'
|
jpayne@69
|
367 path = _jython_aware_splitext(os.path.normpath(path))
|
jpayne@69
|
368
|
jpayne@69
|
369 _relpath = os.path.relpath(path, self._top_level_dir)
|
jpayne@69
|
370 assert not os.path.isabs(_relpath), "Path must be within the project"
|
jpayne@69
|
371 assert not _relpath.startswith('..'), "Path must be within the project"
|
jpayne@69
|
372
|
jpayne@69
|
373 name = _relpath.replace(os.path.sep, '.')
|
jpayne@69
|
374 return name
|
jpayne@69
|
375
|
jpayne@69
|
376 def _get_module_from_name(self, name):
|
jpayne@69
|
377 __import__(name)
|
jpayne@69
|
378 return sys.modules[name]
|
jpayne@69
|
379
|
jpayne@69
|
380 def _match_path(self, path, full_path, pattern):
|
jpayne@69
|
381 # override this method to use alternative matching strategy
|
jpayne@69
|
382 return fnmatch(path, pattern)
|
jpayne@69
|
383
|
jpayne@69
|
384 def _find_tests(self, start_dir, pattern, namespace=False):
|
jpayne@69
|
385 """Used by discovery. Yields test suites it loads."""
|
jpayne@69
|
386 # Handle the __init__ in this package
|
jpayne@69
|
387 name = self._get_name_from_path(start_dir)
|
jpayne@69
|
388 # name is '.' when start_dir == top_level_dir (and top_level_dir is by
|
jpayne@69
|
389 # definition not a package).
|
jpayne@69
|
390 if name != '.' and name not in self._loading_packages:
|
jpayne@69
|
391 # name is in self._loading_packages while we have called into
|
jpayne@69
|
392 # loadTestsFromModule with name.
|
jpayne@69
|
393 tests, should_recurse = self._find_test_path(
|
jpayne@69
|
394 start_dir, pattern, namespace)
|
jpayne@69
|
395 if tests is not None:
|
jpayne@69
|
396 yield tests
|
jpayne@69
|
397 if not should_recurse:
|
jpayne@69
|
398 # Either an error occurred, or load_tests was used by the
|
jpayne@69
|
399 # package.
|
jpayne@69
|
400 return
|
jpayne@69
|
401 # Handle the contents.
|
jpayne@69
|
402 paths = sorted(os.listdir(start_dir))
|
jpayne@69
|
403 for path in paths:
|
jpayne@69
|
404 full_path = os.path.join(start_dir, path)
|
jpayne@69
|
405 tests, should_recurse = self._find_test_path(
|
jpayne@69
|
406 full_path, pattern, namespace)
|
jpayne@69
|
407 if tests is not None:
|
jpayne@69
|
408 yield tests
|
jpayne@69
|
409 if should_recurse:
|
jpayne@69
|
410 # we found a package that didn't use load_tests.
|
jpayne@69
|
411 name = self._get_name_from_path(full_path)
|
jpayne@69
|
412 self._loading_packages.add(name)
|
jpayne@69
|
413 try:
|
jpayne@69
|
414 yield from self._find_tests(full_path, pattern, namespace)
|
jpayne@69
|
415 finally:
|
jpayne@69
|
416 self._loading_packages.discard(name)
|
jpayne@69
|
417
|
jpayne@69
|
418 def _find_test_path(self, full_path, pattern, namespace=False):
|
jpayne@69
|
419 """Used by discovery.
|
jpayne@69
|
420
|
jpayne@69
|
421 Loads tests from a single file, or a directories' __init__.py when
|
jpayne@69
|
422 passed the directory.
|
jpayne@69
|
423
|
jpayne@69
|
424 Returns a tuple (None_or_tests_from_file, should_recurse).
|
jpayne@69
|
425 """
|
jpayne@69
|
426 basename = os.path.basename(full_path)
|
jpayne@69
|
427 if os.path.isfile(full_path):
|
jpayne@69
|
428 if not VALID_MODULE_NAME.match(basename):
|
jpayne@69
|
429 # valid Python identifiers only
|
jpayne@69
|
430 return None, False
|
jpayne@69
|
431 if not self._match_path(basename, full_path, pattern):
|
jpayne@69
|
432 return None, False
|
jpayne@69
|
433 # if the test file matches, load it
|
jpayne@69
|
434 name = self._get_name_from_path(full_path)
|
jpayne@69
|
435 try:
|
jpayne@69
|
436 module = self._get_module_from_name(name)
|
jpayne@69
|
437 except case.SkipTest as e:
|
jpayne@69
|
438 return _make_skipped_test(name, e, self.suiteClass), False
|
jpayne@69
|
439 except:
|
jpayne@69
|
440 error_case, error_message = \
|
jpayne@69
|
441 _make_failed_import_test(name, self.suiteClass)
|
jpayne@69
|
442 self.errors.append(error_message)
|
jpayne@69
|
443 return error_case, False
|
jpayne@69
|
444 else:
|
jpayne@69
|
445 mod_file = os.path.abspath(
|
jpayne@69
|
446 getattr(module, '__file__', full_path))
|
jpayne@69
|
447 realpath = _jython_aware_splitext(
|
jpayne@69
|
448 os.path.realpath(mod_file))
|
jpayne@69
|
449 fullpath_noext = _jython_aware_splitext(
|
jpayne@69
|
450 os.path.realpath(full_path))
|
jpayne@69
|
451 if realpath.lower() != fullpath_noext.lower():
|
jpayne@69
|
452 module_dir = os.path.dirname(realpath)
|
jpayne@69
|
453 mod_name = _jython_aware_splitext(
|
jpayne@69
|
454 os.path.basename(full_path))
|
jpayne@69
|
455 expected_dir = os.path.dirname(full_path)
|
jpayne@69
|
456 msg = ("%r module incorrectly imported from %r. Expected "
|
jpayne@69
|
457 "%r. Is this module globally installed?")
|
jpayne@69
|
458 raise ImportError(
|
jpayne@69
|
459 msg % (mod_name, module_dir, expected_dir))
|
jpayne@69
|
460 return self.loadTestsFromModule(module, pattern=pattern), False
|
jpayne@69
|
461 elif os.path.isdir(full_path):
|
jpayne@69
|
462 if (not namespace and
|
jpayne@69
|
463 not os.path.isfile(os.path.join(full_path, '__init__.py'))):
|
jpayne@69
|
464 return None, False
|
jpayne@69
|
465
|
jpayne@69
|
466 load_tests = None
|
jpayne@69
|
467 tests = None
|
jpayne@69
|
468 name = self._get_name_from_path(full_path)
|
jpayne@69
|
469 try:
|
jpayne@69
|
470 package = self._get_module_from_name(name)
|
jpayne@69
|
471 except case.SkipTest as e:
|
jpayne@69
|
472 return _make_skipped_test(name, e, self.suiteClass), False
|
jpayne@69
|
473 except:
|
jpayne@69
|
474 error_case, error_message = \
|
jpayne@69
|
475 _make_failed_import_test(name, self.suiteClass)
|
jpayne@69
|
476 self.errors.append(error_message)
|
jpayne@69
|
477 return error_case, False
|
jpayne@69
|
478 else:
|
jpayne@69
|
479 load_tests = getattr(package, 'load_tests', None)
|
jpayne@69
|
480 # Mark this package as being in load_tests (possibly ;))
|
jpayne@69
|
481 self._loading_packages.add(name)
|
jpayne@69
|
482 try:
|
jpayne@69
|
483 tests = self.loadTestsFromModule(package, pattern=pattern)
|
jpayne@69
|
484 if load_tests is not None:
|
jpayne@69
|
485 # loadTestsFromModule(package) has loaded tests for us.
|
jpayne@69
|
486 return tests, False
|
jpayne@69
|
487 return tests, True
|
jpayne@69
|
488 finally:
|
jpayne@69
|
489 self._loading_packages.discard(name)
|
jpayne@69
|
490 else:
|
jpayne@69
|
491 return None, False
|
jpayne@69
|
492
|
jpayne@69
|
493
|
jpayne@69
|
494 defaultTestLoader = TestLoader()
|
jpayne@69
|
495
|
jpayne@69
|
496
|
jpayne@69
|
497 def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
|
jpayne@69
|
498 loader = TestLoader()
|
jpayne@69
|
499 loader.sortTestMethodsUsing = sortUsing
|
jpayne@69
|
500 loader.testMethodPrefix = prefix
|
jpayne@69
|
501 loader.testNamePatterns = testNamePatterns
|
jpayne@69
|
502 if suiteClass:
|
jpayne@69
|
503 loader.suiteClass = suiteClass
|
jpayne@69
|
504 return loader
|
jpayne@69
|
505
|
jpayne@69
|
506 def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
|
jpayne@69
|
507 return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
|
jpayne@69
|
508
|
jpayne@69
|
509 def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
|
jpayne@69
|
510 suiteClass=suite.TestSuite):
|
jpayne@69
|
511 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
|
jpayne@69
|
512 testCaseClass)
|
jpayne@69
|
513
|
jpayne@69
|
514 def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
|
jpayne@69
|
515 suiteClass=suite.TestSuite):
|
jpayne@69
|
516 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
|
jpayne@69
|
517 module)
|