blob: eb03b4ab87707a10e84c944b65805c0ef28d9a01 [file] [log] [blame]
Benjamin Petersonbed7d042009-07-19 21:01:52 +00001"""Loading unittests."""
2
3import os
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00004import re
Benjamin Petersonbed7d042009-07-19 21:01:52 +00005import sys
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00006import traceback
Benjamin Petersonbed7d042009-07-19 21:01:52 +00007import types
Raymond Hettingerc50846a2010-04-05 18:56:31 +00008import functools
Barry Warsawd78742a2014-09-08 14:21:37 -04009import warnings
Benjamin Petersonbed7d042009-07-19 21:01:52 +000010
Jonas Haag5b48dc62017-11-25 16:23:52 +010011from fnmatch import fnmatch, fnmatchcase
Benjamin Petersonbed7d042009-07-19 21:01:52 +000012
13from . import case, suite, util
14
Benjamin Petersondccc1fc2010-03-22 00:15:53 +000015__unittest = True
Benjamin Petersonbed7d042009-07-19 21:01:52 +000016
Brett Cannonf299abd2015-04-13 14:21:02 -040017# what about .pyc (etc)
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000018# we would need to avoid loading the same tests multiple times
Brett Cannonf299abd2015-04-13 14:21:02 -040019# from '.py', *and* '.pyc'
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000020VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
21
22
Antoine Pitroud8337792015-03-18 23:56:46 +010023class _FailedTest(case.TestCase):
24 _testMethodName = None
25
26 def __init__(self, method_name, exception):
27 self._exception = exception
28 super(_FailedTest, self).__init__(method_name)
29
30 def __getattr__(self, name):
31 if name != self._testMethodName:
32 return super(_FailedTest, self).__getattr__(name)
33 def testFailure():
34 raise self._exception
35 return testFailure
36
37
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000038def _make_failed_import_test(name, suiteClass):
Robert Collinsf920c212014-10-20 13:24:05 +130039 message = 'Failed to import test module: %s\n%s' % (
40 name, traceback.format_exc())
Antoine Pitrou8eef6a92015-03-19 00:01:37 +010041 return _make_failed_test(name, ImportError(message), suiteClass, message)
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000042
Benjamin Peterson886af962010-03-21 23:13:07 +000043def _make_failed_load_tests(name, exception, suiteClass):
Robert Collinsf920c212014-10-20 13:24:05 +130044 message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
45 return _make_failed_test(
Antoine Pitrou8eef6a92015-03-19 00:01:37 +010046 name, exception, suiteClass, message)
Benjamin Peterson886af962010-03-21 23:13:07 +000047
Antoine Pitrou8eef6a92015-03-19 00:01:37 +010048def _make_failed_test(methodname, exception, suiteClass, message):
Antoine Pitroud8337792015-03-18 23:56:46 +010049 test = _FailedTest(methodname, exception)
Antoine Pitrou8eef6a92015-03-19 00:01:37 +010050 return suiteClass((test,)), message
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000051
Ezio Melottieae2b382013-03-01 14:47:50 +020052def _make_skipped_test(methodname, exception, suiteClass):
53 @case.skip(str(exception))
54 def testSkipped(self):
55 pass
56 attrs = {methodname: testSkipped}
57 TestClass = type("ModuleSkipped", (case.TestCase,), attrs)
58 return suiteClass((TestClass(methodname),))
59
Michael Foorde01c62c2012-03-13 00:09:54 -070060def _jython_aware_splitext(path):
61 if path.lower().endswith('$py.class'):
62 return path[:-9]
63 return os.path.splitext(path)[0]
64
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000065
Benjamin Petersonbed7d042009-07-19 21:01:52 +000066class TestLoader(object):
67 """
68 This class is responsible for loading tests according to various criteria
69 and returning them wrapped in a TestSuite
70 """
71 testMethodPrefix = 'test'
72 sortTestMethodsUsing = staticmethod(util.three_way_cmp)
Jonas Haag5b48dc62017-11-25 16:23:52 +010073 testNamePatterns = None
Benjamin Petersonbed7d042009-07-19 21:01:52 +000074 suiteClass = suite.TestSuite
75 _top_level_dir = None
76
Robert Collinsf920c212014-10-20 13:24:05 +130077 def __init__(self):
78 super(TestLoader, self).__init__()
79 self.errors = []
Robert Collinsbf2bda32014-11-05 03:09:01 +130080 # Tracks packages which we have called into via load_tests, to
81 # avoid infinite re-entrancy.
82 self._loading_packages = set()
Robert Collinsf920c212014-10-20 13:24:05 +130083
Benjamin Petersonbed7d042009-07-19 21:01:52 +000084 def loadTestsFromTestCase(self, testCaseClass):
Martin Panter37f183d2017-01-18 12:06:38 +000085 """Return a suite of all test cases contained in testCaseClass"""
Benjamin Petersonbed7d042009-07-19 21:01:52 +000086 if issubclass(testCaseClass, suite.TestSuite):
Michael Foorde28bb152013-11-23 13:29:23 +000087 raise TypeError("Test cases should not be derived from "
88 "TestSuite. Maybe you meant to derive from "
89 "TestCase?")
Benjamin Petersonbed7d042009-07-19 21:01:52 +000090 testCaseNames = self.getTestCaseNames(testCaseClass)
91 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
92 testCaseNames = ['runTest']
93 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
94 return loaded_suite
95
Barry Warsawd78742a2014-09-08 14:21:37 -040096 # XXX After Python 3.5, remove backward compatibility hacks for
97 # use_load_tests deprecation via *args and **kws. See issue 16662.
98 def loadTestsFromModule(self, module, *args, pattern=None, **kws):
Martin Panter37f183d2017-01-18 12:06:38 +000099 """Return a suite of all test cases contained in the given module"""
Barry Warsawd78742a2014-09-08 14:21:37 -0400100 # This method used to take an undocumented and unofficial
101 # use_load_tests argument. For backward compatibility, we still
102 # accept the argument (which can also be the first position) but we
103 # ignore it and issue a deprecation warning if it's present.
Barry Warsawbb1e3f12014-09-08 17:29:02 -0400104 if len(args) > 0 or 'use_load_tests' in kws:
Barry Warsawd78742a2014-09-08 14:21:37 -0400105 warnings.warn('use_load_tests is deprecated and ignored',
106 DeprecationWarning)
107 kws.pop('use_load_tests', None)
108 if len(args) > 1:
Barry Warsawbb1e3f12014-09-08 17:29:02 -0400109 # Complain about the number of arguments, but don't forget the
110 # required `module` argument.
111 complaint = len(args) + 1
112 raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
Barry Warsawd78742a2014-09-08 14:21:37 -0400113 if len(kws) != 0:
114 # Since the keyword arguments are unsorted (see PEP 468), just
115 # pick the alphabetically sorted first argument to complain about,
116 # if multiple were given. At least the error message will be
117 # predictable.
118 complaint = sorted(kws)[0]
119 raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000120 tests = []
121 for name in dir(module):
122 obj = getattr(module, name)
123 if isinstance(obj, type) and issubclass(obj, case.TestCase):
124 tests.append(self.loadTestsFromTestCase(obj))
125
126 load_tests = getattr(module, 'load_tests', None)
Michael Foord41647d62010-02-06 00:26:13 +0000127 tests = self.suiteClass(tests)
Barry Warsawd78742a2014-09-08 14:21:37 -0400128 if load_tests is not None:
Benjamin Peterson886af962010-03-21 23:13:07 +0000129 try:
Barry Warsawd78742a2014-09-08 14:21:37 -0400130 return load_tests(self, tests, pattern)
Benjamin Peterson886af962010-03-21 23:13:07 +0000131 except Exception as e:
Robert Collinsf920c212014-10-20 13:24:05 +1300132 error_case, error_message = _make_failed_load_tests(
133 module.__name__, e, self.suiteClass)
134 self.errors.append(error_message)
135 return error_case
Michael Foord41647d62010-02-06 00:26:13 +0000136 return tests
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000137
138 def loadTestsFromName(self, name, module=None):
Martin Panter37f183d2017-01-18 12:06:38 +0000139 """Return a suite of all test cases given a string specifier.
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000140
141 The name may resolve either to a module, a test case class, a
142 test method within a test case class, or a callable object which
143 returns a TestCase or TestSuite instance.
144
145 The method optionally resolves the names relative to a given module.
146 """
147 parts = name.split('.')
Robert Collins659dd622014-10-30 08:27:27 +1300148 error_case, error_message = None, None
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000149 if module is None:
150 parts_copy = parts[:]
151 while parts_copy:
152 try:
Robert Collins659dd622014-10-30 08:27:27 +1300153 module_name = '.'.join(parts_copy)
154 module = __import__(module_name)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000155 break
156 except ImportError:
Robert Collins659dd622014-10-30 08:27:27 +1300157 next_attribute = parts_copy.pop()
158 # Last error so we can give it to the user if needed.
159 error_case, error_message = _make_failed_import_test(
160 next_attribute, self.suiteClass)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000161 if not parts_copy:
Robert Collins659dd622014-10-30 08:27:27 +1300162 # Even the top level import failed: report that error.
163 self.errors.append(error_message)
164 return error_case
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000165 parts = parts[1:]
166 obj = module
167 for part in parts:
Robert Collins659dd622014-10-30 08:27:27 +1300168 try:
169 parent, obj = obj, getattr(obj, part)
170 except AttributeError as e:
171 # We can't traverse some part of the name.
172 if (getattr(obj, '__path__', None) is not None
173 and error_case is not None):
174 # This is a package (no __path__ per importlib docs), and we
175 # encountered an error importing something. We cannot tell
176 # the difference between package.WrongNameTestClass and
177 # package.wrong_module_name so we just report the
178 # ImportError - it is more informative.
179 self.errors.append(error_message)
180 return error_case
181 else:
182 # Otherwise, we signal that an AttributeError has occurred.
183 error_case, error_message = _make_failed_test(
Antoine Pitrou8eef6a92015-03-19 00:01:37 +0100184 part, e, self.suiteClass,
Robert Collins659dd622014-10-30 08:27:27 +1300185 'Failed to access attribute:\n%s' % (
186 traceback.format_exc(),))
187 self.errors.append(error_message)
188 return error_case
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000189
190 if isinstance(obj, types.ModuleType):
191 return self.loadTestsFromModule(obj)
192 elif isinstance(obj, type) and issubclass(obj, case.TestCase):
193 return self.loadTestsFromTestCase(obj)
194 elif (isinstance(obj, types.FunctionType) and
195 isinstance(parent, type) and
196 issubclass(parent, case.TestCase)):
R David Murray5e2f5932013-04-11 08:55:45 -0400197 name = parts[-1]
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000198 inst = parent(name)
199 # static methods follow a different path
200 if not isinstance(getattr(inst, name), types.FunctionType):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000201 return self.suiteClass([inst])
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000202 elif isinstance(obj, suite.TestSuite):
203 return obj
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200204 if callable(obj):
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000205 test = obj()
206 if isinstance(test, suite.TestSuite):
207 return test
208 elif isinstance(test, case.TestCase):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000209 return self.suiteClass([test])
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000210 else:
211 raise TypeError("calling %s returned %s, not a test" %
212 (obj, test))
213 else:
214 raise TypeError("don't know how to make test from: %s" % obj)
215
216 def loadTestsFromNames(self, names, module=None):
Martin Panter37f183d2017-01-18 12:06:38 +0000217 """Return a suite of all test cases found using the given sequence
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000218 of string specifiers. See 'loadTestsFromName()'.
219 """
220 suites = [self.loadTestsFromName(name, module) for name in names]
221 return self.suiteClass(suites)
222
223 def getTestCaseNames(self, testCaseClass):
224 """Return a sorted sequence of method names found within testCaseClass
225 """
Jonas Haag5b48dc62017-11-25 16:23:52 +0100226 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)))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000235 if self.sortTestMethodsUsing:
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000236 testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000237 return testFnNames
238
239 def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
240 """Find and return all test modules from the specified start
Michael Foord6bcfade2010-11-20 17:22:21 +0000241 directory, recursing into subdirectories to find them and return all
242 tests found within them. Only test files that match the pattern will
243 be loaded. (Using shell style pattern matching.)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000244
245 All test modules must be importable from the top level of the project.
246 If the start directory is not the top level directory then the top
247 level directory must be specified separately.
248
249 If a test package name (directory with '__init__.py') matches the
250 pattern then the package will be checked for a 'load_tests' function. If
Robert Collinsbf2bda32014-11-05 03:09:01 +1300251 this exists then it will be called with (loader, tests, pattern) unless
252 the package has already had load_tests called from the same discovery
253 invocation, in which case the package module object is not scanned for
254 tests - this ensures that when a package uses discover to further
255 discover child tests that infinite recursion does not happen.
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000256
Robert Collinsbf2bda32014-11-05 03:09:01 +1300257 If load_tests exists then discovery does *not* recurse into the package,
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000258 load_tests is responsible for loading all tests in the package.
259
260 The pattern is deliberately not stored as a loader attribute so that
261 packages can continue discovery themselves. top_level_dir is stored so
262 load_tests does not need to pass this argument in to loader.discover().
Michael Foord80cbc9e2013-03-18 17:50:12 -0700263
264 Paths are sorted before being imported to ensure reproducible execution
265 order even on filesystems with non-alphabetical ordering like ext3/4.
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000266 """
Benjamin Petersonb48af542010-04-11 20:43:16 +0000267 set_implicit_top = False
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000268 if top_level_dir is None and self._top_level_dir is not None:
269 # make top_level_dir optional if called from load_tests in a package
270 top_level_dir = self._top_level_dir
271 elif top_level_dir is None:
Benjamin Petersonb48af542010-04-11 20:43:16 +0000272 set_implicit_top = True
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000273 top_level_dir = start_dir
274
Benjamin Petersonb48af542010-04-11 20:43:16 +0000275 top_level_dir = os.path.abspath(top_level_dir)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000276
277 if not top_level_dir in sys.path:
278 # all test modules must be importable from the top level directory
Michael Foord3b2494f2010-05-07 23:42:40 +0000279 # should we *unconditionally* put the start directory in first
280 # in sys.path to minimise likelihood of conflicts between installed
281 # modules and development versions?
282 sys.path.insert(0, top_level_dir)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000283 self._top_level_dir = top_level_dir
284
Benjamin Petersonb48af542010-04-11 20:43:16 +0000285 is_not_importable = False
Michael Foorde28bb152013-11-23 13:29:23 +0000286 is_namespace = False
287 tests = []
Benjamin Petersonb48af542010-04-11 20:43:16 +0000288 if os.path.isdir(os.path.abspath(start_dir)):
289 start_dir = os.path.abspath(start_dir)
290 if start_dir != top_level_dir:
291 is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
292 else:
293 # support for discovery from dotted module names
294 try:
295 __import__(start_dir)
296 except ImportError:
297 is_not_importable = True
298 else:
299 the_module = sys.modules[start_dir]
300 top_part = start_dir.split('.')[0]
Michael Foorde28bb152013-11-23 13:29:23 +0000301 try:
302 start_dir = os.path.abspath(
303 os.path.dirname((the_module.__file__)))
304 except AttributeError:
305 # look for namespace packages
306 try:
307 spec = the_module.__spec__
308 except AttributeError:
309 spec = None
310
311 if spec and spec.loader is None:
312 if spec.submodule_search_locations is not None:
313 is_namespace = True
314
315 for path in the_module.__path__:
316 if (not set_implicit_top and
317 not path.startswith(top_level_dir)):
318 continue
319 self._top_level_dir = \
320 (path.split(the_module.__name__
321 .replace(".", os.path.sep))[0])
322 tests.extend(self._find_tests(path,
323 pattern,
324 namespace=True))
325 elif the_module.__name__ in sys.builtin_module_names:
326 # builtin module
327 raise TypeError('Can not use builtin modules '
328 'as dotted module names') from None
329 else:
330 raise TypeError(
331 'don\'t know how to discover from {!r}'
332 .format(the_module)) from None
333
Benjamin Petersonb48af542010-04-11 20:43:16 +0000334 if set_implicit_top:
Michael Foorde28bb152013-11-23 13:29:23 +0000335 if not is_namespace:
336 self._top_level_dir = \
337 self._get_directory_containing_module(top_part)
338 sys.path.remove(top_level_dir)
339 else:
340 sys.path.remove(top_level_dir)
Benjamin Petersonb48af542010-04-11 20:43:16 +0000341
342 if is_not_importable:
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000343 raise ImportError('Start directory is not importable: %r' % start_dir)
344
Michael Foorde28bb152013-11-23 13:29:23 +0000345 if not is_namespace:
346 tests = list(self._find_tests(start_dir, pattern))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000347 return self.suiteClass(tests)
348
Benjamin Petersonb48af542010-04-11 20:43:16 +0000349 def _get_directory_containing_module(self, module_name):
350 module = sys.modules[module_name]
351 full_path = os.path.abspath(module.__file__)
352
353 if os.path.basename(full_path).lower().startswith('__init__.py'):
354 return os.path.dirname(os.path.dirname(full_path))
355 else:
356 # here we have been given a module rather than a package - so
357 # all we can do is search the *same* directory the module is in
358 # should an exception be raised instead
359 return os.path.dirname(full_path)
360
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000361 def _get_name_from_path(self, path):
Robert Collins68b11d12014-11-05 03:43:36 +1300362 if path == self._top_level_dir:
363 return '.'
Michael Foorde01c62c2012-03-13 00:09:54 -0700364 path = _jython_aware_splitext(os.path.normpath(path))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000365
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000366 _relpath = os.path.relpath(path, self._top_level_dir)
367 assert not os.path.isabs(_relpath), "Path must be within the project"
368 assert not _relpath.startswith('..'), "Path must be within the project"
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000369
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000370 name = _relpath.replace(os.path.sep, '.')
371 return name
372
373 def _get_module_from_name(self, name):
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000374 __import__(name)
375 return sys.modules[name]
376
Michael Foord4107d312010-06-05 10:45:41 +0000377 def _match_path(self, path, full_path, pattern):
378 # override this method to use alternative matching strategy
379 return fnmatch(path, pattern)
380
Michael Foorde28bb152013-11-23 13:29:23 +0000381 def _find_tests(self, start_dir, pattern, namespace=False):
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000382 """Used by discovery. Yields test suites it loads."""
Robert Collinsbf2bda32014-11-05 03:09:01 +1300383 # Handle the __init__ in this package
384 name = self._get_name_from_path(start_dir)
385 # name is '.' when start_dir == top_level_dir (and top_level_dir is by
386 # definition not a package).
387 if name != '.' and name not in self._loading_packages:
388 # name is in self._loading_packages while we have called into
389 # loadTestsFromModule with name.
390 tests, should_recurse = self._find_test_path(
391 start_dir, pattern, namespace)
392 if tests is not None:
393 yield tests
394 if not should_recurse:
Martin Panter46f50722016-05-26 05:35:26 +0000395 # Either an error occurred, or load_tests was used by the
Robert Collinsbf2bda32014-11-05 03:09:01 +1300396 # package.
397 return
398 # Handle the contents.
Michael Foord80cbc9e2013-03-18 17:50:12 -0700399 paths = sorted(os.listdir(start_dir))
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000400 for path in paths:
401 full_path = os.path.join(start_dir, path)
Robert Collinsbf2bda32014-11-05 03:09:01 +1300402 tests, should_recurse = self._find_test_path(
403 full_path, pattern, namespace)
404 if tests is not None:
405 yield tests
406 if should_recurse:
407 # we found a package that didn't use load_tests.
Michael Foord4107d312010-06-05 10:45:41 +0000408 name = self._get_name_from_path(full_path)
Robert Collinsbf2bda32014-11-05 03:09:01 +1300409 self._loading_packages.add(name)
Michael Foord4107d312010-06-05 10:45:41 +0000410 try:
Robert Collinsbf2bda32014-11-05 03:09:01 +1300411 yield from self._find_tests(full_path, pattern, namespace)
412 finally:
413 self._loading_packages.discard(name)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000414
Robert Collinsbf2bda32014-11-05 03:09:01 +1300415 def _find_test_path(self, full_path, pattern, namespace=False):
416 """Used by discovery.
417
418 Loads tests from a single file, or a directories' __init__.py when
419 passed the directory.
420
421 Returns a tuple (None_or_tests_from_file, should_recurse).
422 """
423 basename = os.path.basename(full_path)
424 if os.path.isfile(full_path):
425 if not VALID_MODULE_NAME.match(basename):
426 # valid Python identifiers only
427 return None, False
428 if not self._match_path(basename, full_path, pattern):
429 return None, False
430 # if the test file matches, load it
431 name = self._get_name_from_path(full_path)
432 try:
433 module = self._get_module_from_name(name)
434 except case.SkipTest as e:
435 return _make_skipped_test(name, e, self.suiteClass), False
436 except:
437 error_case, error_message = \
438 _make_failed_import_test(name, self.suiteClass)
439 self.errors.append(error_message)
440 return error_case, False
441 else:
442 mod_file = os.path.abspath(
443 getattr(module, '__file__', full_path))
444 realpath = _jython_aware_splitext(
445 os.path.realpath(mod_file))
446 fullpath_noext = _jython_aware_splitext(
447 os.path.realpath(full_path))
448 if realpath.lower() != fullpath_noext.lower():
449 module_dir = os.path.dirname(realpath)
450 mod_name = _jython_aware_splitext(
451 os.path.basename(full_path))
452 expected_dir = os.path.dirname(full_path)
453 msg = ("%r module incorrectly imported from %r. Expected "
454 "%r. Is this module globally installed?")
455 raise ImportError(
456 msg % (mod_name, module_dir, expected_dir))
457 return self.loadTestsFromModule(module, pattern=pattern), False
458 elif os.path.isdir(full_path):
459 if (not namespace and
460 not os.path.isfile(os.path.join(full_path, '__init__.py'))):
461 return None, False
462
463 load_tests = None
464 tests = None
465 name = self._get_name_from_path(full_path)
466 try:
467 package = self._get_module_from_name(name)
468 except case.SkipTest as e:
469 return _make_skipped_test(name, e, self.suiteClass), False
470 except:
471 error_case, error_message = \
472 _make_failed_import_test(name, self.suiteClass)
473 self.errors.append(error_message)
474 return error_case, False
475 else:
476 load_tests = getattr(package, 'load_tests', None)
477 # Mark this package as being in load_tests (possibly ;))
478 self._loading_packages.add(name)
Barry Warsawd78742a2014-09-08 14:21:37 -0400479 try:
Barry Warsawd78742a2014-09-08 14:21:37 -0400480 tests = self.loadTestsFromModule(package, pattern=pattern)
Barry Warsawd78742a2014-09-08 14:21:37 -0400481 if load_tests is not None:
Robert Collinsbf2bda32014-11-05 03:09:01 +1300482 # loadTestsFromModule(package) has loaded tests for us.
483 return tests, False
484 return tests, True
485 finally:
486 self._loading_packages.discard(name)
Robert Collinsecd53832016-03-15 13:29:17 +1300487 else:
488 return None, False
Barry Warsawd78742a2014-09-08 14:21:37 -0400489
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000490
491defaultTestLoader = TestLoader()
492
493
Jonas Haag5b48dc62017-11-25 16:23:52 +0100494def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None):
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000495 loader = TestLoader()
496 loader.sortTestMethodsUsing = sortUsing
497 loader.testMethodPrefix = prefix
Jonas Haag5b48dc62017-11-25 16:23:52 +0100498 loader.testNamePatterns = testNamePatterns
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000499 if suiteClass:
500 loader.suiteClass = suiteClass
501 return loader
502
Jonas Haag5b48dc62017-11-25 16:23:52 +0100503def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None):
504 return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass)
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000505
506def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
507 suiteClass=suite.TestSuite):
508 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
509 testCaseClass)
510
511def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
512 suiteClass=suite.TestSuite):
513 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
514 module)