annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/unittest/loader.py @ 68:5028fdace37b

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