Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 1 | """Loading unittests.""" |
| 2 | |
| 3 | import os |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 4 | import re |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 5 | import sys |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 6 | import traceback |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 7 | import types |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 8 | import functools |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 9 | import warnings |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 10 | |
| 11 | from fnmatch import fnmatch |
| 12 | |
| 13 | from . import case, suite, util |
| 14 | |
Benjamin Peterson | dccc1fc | 2010-03-22 00:15:53 +0000 | [diff] [blame] | 15 | __unittest = True |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 16 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 17 | # what about .pyc or .pyo (etc) |
| 18 | # we would need to avoid loading the same tests multiple times |
| 19 | # from '.py', '.pyc' *and* '.pyo' |
| 20 | VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE) |
| 21 | |
| 22 | |
| 23 | def _make_failed_import_test(name, suiteClass): |
Benjamin Peterson | 434ae77 | 2010-03-22 01:46:47 +0000 | [diff] [blame] | 24 | message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc()) |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 25 | return _make_failed_test('ModuleImportFailure', name, ImportError(message), |
| 26 | suiteClass) |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 27 | |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 28 | def _make_failed_load_tests(name, exception, suiteClass): |
| 29 | return _make_failed_test('LoadTestsFailure', name, exception, suiteClass) |
| 30 | |
| 31 | def _make_failed_test(classname, methodname, exception, suiteClass): |
| 32 | def testFailure(self): |
| 33 | raise exception |
| 34 | attrs = {methodname: testFailure} |
| 35 | TestClass = type(classname, (case.TestCase,), attrs) |
| 36 | return suiteClass((TestClass(methodname),)) |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 37 | |
Ezio Melotti | eae2b38 | 2013-03-01 14:47:50 +0200 | [diff] [blame] | 38 | def _make_skipped_test(methodname, exception, suiteClass): |
| 39 | @case.skip(str(exception)) |
| 40 | def testSkipped(self): |
| 41 | pass |
| 42 | attrs = {methodname: testSkipped} |
| 43 | TestClass = type("ModuleSkipped", (case.TestCase,), attrs) |
| 44 | return suiteClass((TestClass(methodname),)) |
| 45 | |
Michael Foord | e01c62c | 2012-03-13 00:09:54 -0700 | [diff] [blame] | 46 | def _jython_aware_splitext(path): |
| 47 | if path.lower().endswith('$py.class'): |
| 48 | return path[:-9] |
| 49 | return os.path.splitext(path)[0] |
| 50 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 51 | |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 52 | class TestLoader(object): |
| 53 | """ |
| 54 | This class is responsible for loading tests according to various criteria |
| 55 | and returning them wrapped in a TestSuite |
| 56 | """ |
| 57 | testMethodPrefix = 'test' |
| 58 | sortTestMethodsUsing = staticmethod(util.three_way_cmp) |
| 59 | suiteClass = suite.TestSuite |
| 60 | _top_level_dir = None |
| 61 | |
| 62 | def loadTestsFromTestCase(self, testCaseClass): |
| 63 | """Return a suite of all tests cases contained in testCaseClass""" |
| 64 | if issubclass(testCaseClass, suite.TestSuite): |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 65 | raise TypeError("Test cases should not be derived from " |
| 66 | "TestSuite. Maybe you meant to derive from " |
| 67 | "TestCase?") |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 68 | testCaseNames = self.getTestCaseNames(testCaseClass) |
| 69 | if not testCaseNames and hasattr(testCaseClass, 'runTest'): |
| 70 | testCaseNames = ['runTest'] |
| 71 | loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) |
| 72 | return loaded_suite |
| 73 | |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 74 | # XXX After Python 3.5, remove backward compatibility hacks for |
| 75 | # use_load_tests deprecation via *args and **kws. See issue 16662. |
| 76 | def loadTestsFromModule(self, module, *args, pattern=None, **kws): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 77 | """Return a suite of all tests cases contained in the given module""" |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 78 | # This method used to take an undocumented and unofficial |
| 79 | # use_load_tests argument. For backward compatibility, we still |
| 80 | # accept the argument (which can also be the first position) but we |
| 81 | # ignore it and issue a deprecation warning if it's present. |
Barry Warsaw | bb1e3f1 | 2014-09-08 17:29:02 -0400 | [diff] [blame] | 82 | if len(args) > 0 or 'use_load_tests' in kws: |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 83 | warnings.warn('use_load_tests is deprecated and ignored', |
| 84 | DeprecationWarning) |
| 85 | kws.pop('use_load_tests', None) |
| 86 | if len(args) > 1: |
Barry Warsaw | bb1e3f1 | 2014-09-08 17:29:02 -0400 | [diff] [blame] | 87 | # Complain about the number of arguments, but don't forget the |
| 88 | # required `module` argument. |
| 89 | complaint = len(args) + 1 |
| 90 | raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint)) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 91 | if len(kws) != 0: |
| 92 | # Since the keyword arguments are unsorted (see PEP 468), just |
| 93 | # pick the alphabetically sorted first argument to complain about, |
| 94 | # if multiple were given. At least the error message will be |
| 95 | # predictable. |
| 96 | complaint = sorted(kws)[0] |
| 97 | raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 98 | tests = [] |
| 99 | for name in dir(module): |
| 100 | obj = getattr(module, name) |
| 101 | if isinstance(obj, type) and issubclass(obj, case.TestCase): |
| 102 | tests.append(self.loadTestsFromTestCase(obj)) |
| 103 | |
| 104 | load_tests = getattr(module, 'load_tests', None) |
Michael Foord | 41647d6 | 2010-02-06 00:26:13 +0000 | [diff] [blame] | 105 | tests = self.suiteClass(tests) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 106 | if load_tests is not None: |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 107 | try: |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 108 | return load_tests(self, tests, pattern) |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 109 | except Exception as e: |
| 110 | return _make_failed_load_tests(module.__name__, e, |
| 111 | self.suiteClass) |
Michael Foord | 41647d6 | 2010-02-06 00:26:13 +0000 | [diff] [blame] | 112 | return tests |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 113 | |
| 114 | def loadTestsFromName(self, name, module=None): |
| 115 | """Return a suite of all tests cases given a string specifier. |
| 116 | |
| 117 | The name may resolve either to a module, a test case class, a |
| 118 | test method within a test case class, or a callable object which |
| 119 | returns a TestCase or TestSuite instance. |
| 120 | |
| 121 | The method optionally resolves the names relative to a given module. |
| 122 | """ |
| 123 | parts = name.split('.') |
| 124 | if module is None: |
| 125 | parts_copy = parts[:] |
| 126 | while parts_copy: |
| 127 | try: |
| 128 | module = __import__('.'.join(parts_copy)) |
| 129 | break |
| 130 | except ImportError: |
| 131 | del parts_copy[-1] |
| 132 | if not parts_copy: |
| 133 | raise |
| 134 | parts = parts[1:] |
| 135 | obj = module |
| 136 | for part in parts: |
| 137 | parent, obj = obj, getattr(obj, part) |
| 138 | |
| 139 | if isinstance(obj, types.ModuleType): |
| 140 | return self.loadTestsFromModule(obj) |
| 141 | elif isinstance(obj, type) and issubclass(obj, case.TestCase): |
| 142 | return self.loadTestsFromTestCase(obj) |
| 143 | elif (isinstance(obj, types.FunctionType) and |
| 144 | isinstance(parent, type) and |
| 145 | issubclass(parent, case.TestCase)): |
R David Murray | 5e2f593 | 2013-04-11 08:55:45 -0400 | [diff] [blame] | 146 | name = parts[-1] |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 147 | inst = parent(name) |
| 148 | # static methods follow a different path |
| 149 | if not isinstance(getattr(inst, name), types.FunctionType): |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 150 | return self.suiteClass([inst]) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 151 | elif isinstance(obj, suite.TestSuite): |
| 152 | return obj |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 153 | if callable(obj): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 154 | test = obj() |
| 155 | if isinstance(test, suite.TestSuite): |
| 156 | return test |
| 157 | elif isinstance(test, case.TestCase): |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 158 | return self.suiteClass([test]) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 159 | else: |
| 160 | raise TypeError("calling %s returned %s, not a test" % |
| 161 | (obj, test)) |
| 162 | else: |
| 163 | raise TypeError("don't know how to make test from: %s" % obj) |
| 164 | |
| 165 | def loadTestsFromNames(self, names, module=None): |
| 166 | """Return a suite of all tests cases found using the given sequence |
| 167 | of string specifiers. See 'loadTestsFromName()'. |
| 168 | """ |
| 169 | suites = [self.loadTestsFromName(name, module) for name in names] |
| 170 | return self.suiteClass(suites) |
| 171 | |
| 172 | def getTestCaseNames(self, testCaseClass): |
| 173 | """Return a sorted sequence of method names found within testCaseClass |
| 174 | """ |
| 175 | def isTestMethod(attrname, testCaseClass=testCaseClass, |
| 176 | prefix=self.testMethodPrefix): |
| 177 | return attrname.startswith(prefix) and \ |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 178 | callable(getattr(testCaseClass, attrname)) |
Senthil Kumaran | f27be5c | 2011-11-25 02:08:39 +0800 | [diff] [blame] | 179 | testFnNames = list(filter(isTestMethod, dir(testCaseClass))) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 180 | if self.sortTestMethodsUsing: |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 181 | testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 182 | return testFnNames |
| 183 | |
| 184 | def discover(self, start_dir, pattern='test*.py', top_level_dir=None): |
| 185 | """Find and return all test modules from the specified start |
Michael Foord | 6bcfade | 2010-11-20 17:22:21 +0000 | [diff] [blame] | 186 | directory, recursing into subdirectories to find them and return all |
| 187 | tests found within them. Only test files that match the pattern will |
| 188 | be loaded. (Using shell style pattern matching.) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 189 | |
| 190 | All test modules must be importable from the top level of the project. |
| 191 | If the start directory is not the top level directory then the top |
| 192 | level directory must be specified separately. |
| 193 | |
| 194 | If a test package name (directory with '__init__.py') matches the |
| 195 | pattern then the package will be checked for a 'load_tests' function. If |
| 196 | this exists then it will be called with loader, tests, pattern. |
| 197 | |
| 198 | If load_tests exists then discovery does *not* recurse into the package, |
| 199 | load_tests is responsible for loading all tests in the package. |
| 200 | |
| 201 | The pattern is deliberately not stored as a loader attribute so that |
| 202 | packages can continue discovery themselves. top_level_dir is stored so |
| 203 | load_tests does not need to pass this argument in to loader.discover(). |
Michael Foord | 80cbc9e | 2013-03-18 17:50:12 -0700 | [diff] [blame] | 204 | |
| 205 | Paths are sorted before being imported to ensure reproducible execution |
| 206 | order even on filesystems with non-alphabetical ordering like ext3/4. |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 207 | """ |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 208 | set_implicit_top = False |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 209 | if top_level_dir is None and self._top_level_dir is not None: |
| 210 | # make top_level_dir optional if called from load_tests in a package |
| 211 | top_level_dir = self._top_level_dir |
| 212 | elif top_level_dir is None: |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 213 | set_implicit_top = True |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 214 | top_level_dir = start_dir |
| 215 | |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 216 | top_level_dir = os.path.abspath(top_level_dir) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 217 | |
| 218 | if not top_level_dir in sys.path: |
| 219 | # all test modules must be importable from the top level directory |
Michael Foord | 3b2494f | 2010-05-07 23:42:40 +0000 | [diff] [blame] | 220 | # should we *unconditionally* put the start directory in first |
| 221 | # in sys.path to minimise likelihood of conflicts between installed |
| 222 | # modules and development versions? |
| 223 | sys.path.insert(0, top_level_dir) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 224 | self._top_level_dir = top_level_dir |
| 225 | |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 226 | is_not_importable = False |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 227 | is_namespace = False |
| 228 | tests = [] |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 229 | if os.path.isdir(os.path.abspath(start_dir)): |
| 230 | start_dir = os.path.abspath(start_dir) |
| 231 | if start_dir != top_level_dir: |
| 232 | is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py')) |
| 233 | else: |
| 234 | # support for discovery from dotted module names |
| 235 | try: |
| 236 | __import__(start_dir) |
| 237 | except ImportError: |
| 238 | is_not_importable = True |
| 239 | else: |
| 240 | the_module = sys.modules[start_dir] |
| 241 | top_part = start_dir.split('.')[0] |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 242 | try: |
| 243 | start_dir = os.path.abspath( |
| 244 | os.path.dirname((the_module.__file__))) |
| 245 | except AttributeError: |
| 246 | # look for namespace packages |
| 247 | try: |
| 248 | spec = the_module.__spec__ |
| 249 | except AttributeError: |
| 250 | spec = None |
| 251 | |
| 252 | if spec and spec.loader is None: |
| 253 | if spec.submodule_search_locations is not None: |
| 254 | is_namespace = True |
| 255 | |
| 256 | for path in the_module.__path__: |
| 257 | if (not set_implicit_top and |
| 258 | not path.startswith(top_level_dir)): |
| 259 | continue |
| 260 | self._top_level_dir = \ |
| 261 | (path.split(the_module.__name__ |
| 262 | .replace(".", os.path.sep))[0]) |
| 263 | tests.extend(self._find_tests(path, |
| 264 | pattern, |
| 265 | namespace=True)) |
| 266 | elif the_module.__name__ in sys.builtin_module_names: |
| 267 | # builtin module |
| 268 | raise TypeError('Can not use builtin modules ' |
| 269 | 'as dotted module names') from None |
| 270 | else: |
| 271 | raise TypeError( |
| 272 | 'don\'t know how to discover from {!r}' |
| 273 | .format(the_module)) from None |
| 274 | |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 275 | if set_implicit_top: |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 276 | if not is_namespace: |
| 277 | self._top_level_dir = \ |
| 278 | self._get_directory_containing_module(top_part) |
| 279 | sys.path.remove(top_level_dir) |
| 280 | else: |
| 281 | sys.path.remove(top_level_dir) |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 282 | |
| 283 | if is_not_importable: |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 284 | raise ImportError('Start directory is not importable: %r' % start_dir) |
| 285 | |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 286 | if not is_namespace: |
| 287 | tests = list(self._find_tests(start_dir, pattern)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 288 | return self.suiteClass(tests) |
| 289 | |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 290 | def _get_directory_containing_module(self, module_name): |
| 291 | module = sys.modules[module_name] |
| 292 | full_path = os.path.abspath(module.__file__) |
| 293 | |
| 294 | if os.path.basename(full_path).lower().startswith('__init__.py'): |
| 295 | return os.path.dirname(os.path.dirname(full_path)) |
| 296 | else: |
| 297 | # here we have been given a module rather than a package - so |
| 298 | # all we can do is search the *same* directory the module is in |
| 299 | # should an exception be raised instead |
| 300 | return os.path.dirname(full_path) |
| 301 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 302 | def _get_name_from_path(self, path): |
Michael Foord | e01c62c | 2012-03-13 00:09:54 -0700 | [diff] [blame] | 303 | path = _jython_aware_splitext(os.path.normpath(path)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 304 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 305 | _relpath = os.path.relpath(path, self._top_level_dir) |
| 306 | assert not os.path.isabs(_relpath), "Path must be within the project" |
| 307 | assert not _relpath.startswith('..'), "Path must be within the project" |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 308 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 309 | name = _relpath.replace(os.path.sep, '.') |
| 310 | return name |
| 311 | |
| 312 | def _get_module_from_name(self, name): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 313 | __import__(name) |
| 314 | return sys.modules[name] |
| 315 | |
Michael Foord | 4107d31 | 2010-06-05 10:45:41 +0000 | [diff] [blame] | 316 | def _match_path(self, path, full_path, pattern): |
| 317 | # override this method to use alternative matching strategy |
| 318 | return fnmatch(path, pattern) |
| 319 | |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 320 | def _find_tests(self, start_dir, pattern, namespace=False): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 321 | """Used by discovery. Yields test suites it loads.""" |
Michael Foord | 80cbc9e | 2013-03-18 17:50:12 -0700 | [diff] [blame] | 322 | paths = sorted(os.listdir(start_dir)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 323 | |
| 324 | for path in paths: |
| 325 | full_path = os.path.join(start_dir, path) |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 326 | if os.path.isfile(full_path): |
| 327 | if not VALID_MODULE_NAME.match(path): |
| 328 | # valid Python identifiers only |
| 329 | continue |
Michael Foord | 4107d31 | 2010-06-05 10:45:41 +0000 | [diff] [blame] | 330 | if not self._match_path(path, full_path, pattern): |
| 331 | continue |
| 332 | # if the test file matches, load it |
| 333 | name = self._get_name_from_path(full_path) |
| 334 | try: |
| 335 | module = self._get_module_from_name(name) |
Ezio Melotti | eae2b38 | 2013-03-01 14:47:50 +0200 | [diff] [blame] | 336 | except case.SkipTest as e: |
| 337 | yield _make_skipped_test(name, e, self.suiteClass) |
Michael Foord | 4107d31 | 2010-06-05 10:45:41 +0000 | [diff] [blame] | 338 | except: |
| 339 | yield _make_failed_import_test(name, self.suiteClass) |
| 340 | else: |
| 341 | mod_file = os.path.abspath(getattr(module, '__file__', full_path)) |
Antoine Pitrou | d5d0bc3 | 2013-10-23 19:11:29 +0200 | [diff] [blame] | 342 | realpath = _jython_aware_splitext(os.path.realpath(mod_file)) |
| 343 | fullpath_noext = _jython_aware_splitext(os.path.realpath(full_path)) |
Michael Foord | 4107d31 | 2010-06-05 10:45:41 +0000 | [diff] [blame] | 344 | if realpath.lower() != fullpath_noext.lower(): |
| 345 | module_dir = os.path.dirname(realpath) |
Michael Foord | e01c62c | 2012-03-13 00:09:54 -0700 | [diff] [blame] | 346 | mod_name = _jython_aware_splitext(os.path.basename(full_path)) |
Michael Foord | 4107d31 | 2010-06-05 10:45:41 +0000 | [diff] [blame] | 347 | expected_dir = os.path.dirname(full_path) |
| 348 | msg = ("%r module incorrectly imported from %r. Expected %r. " |
| 349 | "Is this module globally installed?") |
| 350 | raise ImportError(msg % (mod_name, module_dir, expected_dir)) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 351 | yield self.loadTestsFromModule(module, pattern=pattern) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 352 | elif os.path.isdir(full_path): |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 353 | if (not namespace and |
| 354 | not os.path.isfile(os.path.join(full_path, '__init__.py'))): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 355 | continue |
| 356 | |
| 357 | load_tests = None |
| 358 | tests = None |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 359 | name = self._get_name_from_path(full_path) |
| 360 | try: |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 361 | package = self._get_module_from_name(name) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 362 | except case.SkipTest as e: |
| 363 | yield _make_skipped_test(name, e, self.suiteClass) |
| 364 | except: |
| 365 | yield _make_failed_import_test(name, self.suiteClass) |
| 366 | else: |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 367 | load_tests = getattr(package, 'load_tests', None) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 368 | tests = self.loadTestsFromModule(package, pattern=pattern) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 369 | if tests is not None: |
| 370 | # tests loaded from package file |
| 371 | yield tests |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 372 | |
| 373 | if load_tests is not None: |
| 374 | # loadTestsFromModule(package) has load_tests for us. |
| 375 | continue |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 376 | # recurse into the package |
Michael Foord | e28bb15 | 2013-11-23 13:29:23 +0000 | [diff] [blame] | 377 | yield from self._find_tests(full_path, pattern, |
| 378 | namespace=namespace) |
Barry Warsaw | d78742a | 2014-09-08 14:21:37 -0400 | [diff] [blame] | 379 | |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 380 | |
| 381 | defaultTestLoader = TestLoader() |
| 382 | |
| 383 | |
| 384 | def _makeLoader(prefix, sortUsing, suiteClass=None): |
| 385 | loader = TestLoader() |
| 386 | loader.sortTestMethodsUsing = sortUsing |
| 387 | loader.testMethodPrefix = prefix |
| 388 | if suiteClass: |
| 389 | loader.suiteClass = suiteClass |
| 390 | return loader |
| 391 | |
| 392 | def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp): |
| 393 | return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass) |
| 394 | |
| 395 | def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, |
| 396 | suiteClass=suite.TestSuite): |
| 397 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase( |
| 398 | testCaseClass) |
| 399 | |
| 400 | def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp, |
| 401 | suiteClass=suite.TestSuite): |
| 402 | return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\ |
| 403 | module) |