jpayne@68: """Loading unittests.""" jpayne@68: jpayne@68: import os jpayne@68: import re jpayne@68: import sys jpayne@68: import traceback jpayne@68: import types jpayne@68: import functools jpayne@68: import warnings jpayne@68: jpayne@68: from fnmatch import fnmatch, fnmatchcase jpayne@68: jpayne@68: from . import case, suite, util jpayne@68: jpayne@68: __unittest = True jpayne@68: jpayne@68: # what about .pyc (etc) jpayne@68: # we would need to avoid loading the same tests multiple times jpayne@68: # from '.py', *and* '.pyc' jpayne@68: VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE) jpayne@68: jpayne@68: jpayne@68: class _FailedTest(case.TestCase): jpayne@68: _testMethodName = None jpayne@68: jpayne@68: def __init__(self, method_name, exception): jpayne@68: self._exception = exception jpayne@68: super(_FailedTest, self).__init__(method_name) jpayne@68: jpayne@68: def __getattr__(self, name): jpayne@68: if name != self._testMethodName: jpayne@68: return super(_FailedTest, self).__getattr__(name) jpayne@68: def testFailure(): jpayne@68: raise self._exception jpayne@68: return testFailure jpayne@68: jpayne@68: jpayne@68: def _make_failed_import_test(name, suiteClass): jpayne@68: message = 'Failed to import test module: %s\n%s' % ( jpayne@68: name, traceback.format_exc()) jpayne@68: return _make_failed_test(name, ImportError(message), suiteClass, message) jpayne@68: jpayne@68: def _make_failed_load_tests(name, exception, suiteClass): jpayne@68: message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),) jpayne@68: return _make_failed_test( jpayne@68: name, exception, suiteClass, message) jpayne@68: jpayne@68: def _make_failed_test(methodname, exception, suiteClass, message): jpayne@68: test = _FailedTest(methodname, exception) jpayne@68: return suiteClass((test,)), message jpayne@68: jpayne@68: def _make_skipped_test(methodname, exception, suiteClass): jpayne@68: @case.skip(str(exception)) jpayne@68: def testSkipped(self): jpayne@68: pass jpayne@68: attrs = {methodname: testSkipped} jpayne@68: TestClass = type("ModuleSkipped", (case.TestCase,), attrs) jpayne@68: return suiteClass((TestClass(methodname),)) jpayne@68: jpayne@68: def _jython_aware_splitext(path): jpayne@68: if path.lower().endswith('$py.class'): jpayne@68: return path[:-9] jpayne@68: return os.path.splitext(path)[0] jpayne@68: jpayne@68: jpayne@68: class TestLoader(object): jpayne@68: """ jpayne@68: This class is responsible for loading tests according to various criteria jpayne@68: and returning them wrapped in a TestSuite jpayne@68: """ jpayne@68: testMethodPrefix = 'test' jpayne@68: sortTestMethodsUsing = staticmethod(util.three_way_cmp) jpayne@68: testNamePatterns = None jpayne@68: suiteClass = suite.TestSuite jpayne@68: _top_level_dir = None jpayne@68: jpayne@68: def __init__(self): jpayne@68: super(TestLoader, self).__init__() jpayne@68: self.errors = [] jpayne@68: # Tracks packages which we have called into via load_tests, to jpayne@68: # avoid infinite re-entrancy. jpayne@68: self._loading_packages = set() jpayne@68: jpayne@68: def loadTestsFromTestCase(self, testCaseClass): jpayne@68: """Return a suite of all test cases contained in testCaseClass""" jpayne@68: if issubclass(testCaseClass, suite.TestSuite): jpayne@68: raise TypeError("Test cases should not be derived from " jpayne@68: "TestSuite. Maybe you meant to derive from " jpayne@68: "TestCase?") jpayne@68: testCaseNames = self.getTestCaseNames(testCaseClass) jpayne@68: if not testCaseNames and hasattr(testCaseClass, 'runTest'): jpayne@68: testCaseNames = ['runTest'] jpayne@68: loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) jpayne@68: return loaded_suite jpayne@68: jpayne@68: # XXX After Python 3.5, remove backward compatibility hacks for jpayne@68: # use_load_tests deprecation via *args and **kws. See issue 16662. jpayne@68: def loadTestsFromModule(self, module, *args, pattern=None, **kws): jpayne@68: """Return a suite of all test cases contained in the given module""" jpayne@68: # This method used to take an undocumented and unofficial jpayne@68: # use_load_tests argument. For backward compatibility, we still jpayne@68: # accept the argument (which can also be the first position) but we jpayne@68: # ignore it and issue a deprecation warning if it's present. jpayne@68: if len(args) > 0 or 'use_load_tests' in kws: jpayne@68: warnings.warn('use_load_tests is deprecated and ignored', jpayne@68: DeprecationWarning) jpayne@68: kws.pop('use_load_tests', None) jpayne@68: if len(args) > 1: jpayne@68: # Complain about the number of arguments, but don't forget the jpayne@68: # required `module` argument. jpayne@68: complaint = len(args) + 1 jpayne@68: raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint)) jpayne@68: if len(kws) != 0: jpayne@68: # Since the keyword arguments are unsorted (see PEP 468), just jpayne@68: # pick the alphabetically sorted first argument to complain about, jpayne@68: # if multiple were given. At least the error message will be jpayne@68: # predictable. jpayne@68: complaint = sorted(kws)[0] jpayne@68: raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint)) jpayne@68: tests = [] jpayne@68: for name in dir(module): jpayne@68: obj = getattr(module, name) jpayne@68: if isinstance(obj, type) and issubclass(obj, case.TestCase): jpayne@68: tests.append(self.loadTestsFromTestCase(obj)) jpayne@68: jpayne@68: load_tests = getattr(module, 'load_tests', None) jpayne@68: tests = self.suiteClass(tests) jpayne@68: if load_tests is not None: jpayne@68: try: jpayne@68: return load_tests(self, tests, pattern) jpayne@68: except Exception as e: jpayne@68: error_case, error_message = _make_failed_load_tests( jpayne@68: module.__name__, e, self.suiteClass) jpayne@68: self.errors.append(error_message) jpayne@68: return error_case jpayne@68: return tests jpayne@68: jpayne@68: def loadTestsFromName(self, name, module=None): jpayne@68: """Return a suite of all test cases given a string specifier. jpayne@68: jpayne@68: The name may resolve either to a module, a test case class, a jpayne@68: test method within a test case class, or a callable object which jpayne@68: returns a TestCase or TestSuite instance. jpayne@68: jpayne@68: The method optionally resolves the names relative to a given module. jpayne@68: """ jpayne@68: parts = name.split('.') jpayne@68: error_case, error_message = None, None jpayne@68: if module is None: jpayne@68: parts_copy = parts[:] jpayne@68: while parts_copy: jpayne@68: try: jpayne@68: module_name = '.'.join(parts_copy) jpayne@68: module = __import__(module_name) jpayne@68: break jpayne@68: except ImportError: jpayne@68: next_attribute = parts_copy.pop() jpayne@68: # Last error so we can give it to the user if needed. jpayne@68: error_case, error_message = _make_failed_import_test( jpayne@68: next_attribute, self.suiteClass) jpayne@68: if not parts_copy: jpayne@68: # Even the top level import failed: report that error. jpayne@68: self.errors.append(error_message) jpayne@68: return error_case jpayne@68: parts = parts[1:] jpayne@68: obj = module jpayne@68: for part in parts: jpayne@68: try: jpayne@68: parent, obj = obj, getattr(obj, part) jpayne@68: except AttributeError as e: jpayne@68: # We can't traverse some part of the name. jpayne@68: if (getattr(obj, '__path__', None) is not None jpayne@68: and error_case is not None): jpayne@68: # This is a package (no __path__ per importlib docs), and we jpayne@68: # encountered an error importing something. We cannot tell jpayne@68: # the difference between package.WrongNameTestClass and jpayne@68: # package.wrong_module_name so we just report the jpayne@68: # ImportError - it is more informative. jpayne@68: self.errors.append(error_message) jpayne@68: return error_case jpayne@68: else: jpayne@68: # Otherwise, we signal that an AttributeError has occurred. jpayne@68: error_case, error_message = _make_failed_test( jpayne@68: part, e, self.suiteClass, jpayne@68: 'Failed to access attribute:\n%s' % ( jpayne@68: traceback.format_exc(),)) jpayne@68: self.errors.append(error_message) jpayne@68: return error_case jpayne@68: jpayne@68: if isinstance(obj, types.ModuleType): jpayne@68: return self.loadTestsFromModule(obj) jpayne@68: elif isinstance(obj, type) and issubclass(obj, case.TestCase): jpayne@68: return self.loadTestsFromTestCase(obj) jpayne@68: elif (isinstance(obj, types.FunctionType) and jpayne@68: isinstance(parent, type) and jpayne@68: issubclass(parent, case.TestCase)): jpayne@68: name = parts[-1] jpayne@68: inst = parent(name) jpayne@68: # static methods follow a different path jpayne@68: if not isinstance(getattr(inst, name), types.FunctionType): jpayne@68: return self.suiteClass([inst]) jpayne@68: elif isinstance(obj, suite.TestSuite): jpayne@68: return obj jpayne@68: if callable(obj): jpayne@68: test = obj() jpayne@68: if isinstance(test, suite.TestSuite): jpayne@68: return test jpayne@68: elif isinstance(test, case.TestCase): jpayne@68: return self.suiteClass([test]) jpayne@68: else: jpayne@68: raise TypeError("calling %s returned %s, not a test" % jpayne@68: (obj, test)) jpayne@68: else: jpayne@68: raise TypeError("don't know how to make test from: %s" % obj) jpayne@68: jpayne@68: def loadTestsFromNames(self, names, module=None): jpayne@68: """Return a suite of all test cases found using the given sequence jpayne@68: of string specifiers. See 'loadTestsFromName()'. jpayne@68: """ jpayne@68: suites = [self.loadTestsFromName(name, module) for name in names] jpayne@68: return self.suiteClass(suites) jpayne@68: jpayne@68: def getTestCaseNames(self, testCaseClass): jpayne@68: """Return a sorted sequence of method names found within testCaseClass jpayne@68: """ jpayne@68: def shouldIncludeMethod(attrname): jpayne@68: if not attrname.startswith(self.testMethodPrefix): jpayne@68: return False jpayne@68: testFunc = getattr(testCaseClass, attrname) jpayne@68: if not callable(testFunc): jpayne@68: return False jpayne@68: fullName = f'%s.%s.%s' % ( jpayne@68: testCaseClass.__module__, testCaseClass.__qualname__, attrname jpayne@68: ) jpayne@68: return self.testNamePatterns is None or \ jpayne@68: any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns) jpayne@68: testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass))) jpayne@68: if self.sortTestMethodsUsing: jpayne@68: testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing)) jpayne@68: return testFnNames jpayne@68: jpayne@68: def discover(self, start_dir, pattern='test*.py', top_level_dir=None): jpayne@68: """Find and return all test modules from the specified start jpayne@68: directory, recursing into subdirectories to find them and return all jpayne@68: tests found within them. Only test files that match the pattern will jpayne@68: be loaded. (Using shell style pattern matching.) jpayne@68: jpayne@68: All test modules must be importable from the top level of the project. jpayne@68: If the start directory is not the top level directory then the top jpayne@68: level directory must be specified separately. jpayne@68: jpayne@68: If a test package name (directory with '__init__.py') matches the jpayne@68: pattern then the package will be checked for a 'load_tests' function. If jpayne@68: this exists then it will be called with (loader, tests, pattern) unless jpayne@68: the package has already had load_tests called from the same discovery jpayne@68: invocation, in which case the package module object is not scanned for jpayne@68: tests - this ensures that when a package uses discover to further jpayne@68: discover child tests that infinite recursion does not happen. jpayne@68: jpayne@68: If load_tests exists then discovery does *not* recurse into the package, jpayne@68: load_tests is responsible for loading all tests in the package. jpayne@68: jpayne@68: The pattern is deliberately not stored as a loader attribute so that jpayne@68: packages can continue discovery themselves. top_level_dir is stored so jpayne@68: load_tests does not need to pass this argument in to loader.discover(). jpayne@68: jpayne@68: Paths are sorted before being imported to ensure reproducible execution jpayne@68: order even on filesystems with non-alphabetical ordering like ext3/4. jpayne@68: """ jpayne@68: set_implicit_top = False jpayne@68: if top_level_dir is None and self._top_level_dir is not None: jpayne@68: # make top_level_dir optional if called from load_tests in a package jpayne@68: top_level_dir = self._top_level_dir jpayne@68: elif top_level_dir is None: jpayne@68: set_implicit_top = True jpayne@68: top_level_dir = start_dir jpayne@68: jpayne@68: top_level_dir = os.path.abspath(top_level_dir) jpayne@68: jpayne@68: if not top_level_dir in sys.path: jpayne@68: # all test modules must be importable from the top level directory jpayne@68: # should we *unconditionally* put the start directory in first jpayne@68: # in sys.path to minimise likelihood of conflicts between installed jpayne@68: # modules and development versions? jpayne@68: sys.path.insert(0, top_level_dir) jpayne@68: self._top_level_dir = top_level_dir jpayne@68: jpayne@68: is_not_importable = False jpayne@68: is_namespace = False jpayne@68: tests = [] jpayne@68: if os.path.isdir(os.path.abspath(start_dir)): jpayne@68: start_dir = os.path.abspath(start_dir) jpayne@68: if start_dir != top_level_dir: jpayne@68: is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py')) jpayne@68: else: jpayne@68: # support for discovery from dotted module names jpayne@68: try: jpayne@68: __import__(start_dir) jpayne@68: except ImportError: jpayne@68: is_not_importable = True jpayne@68: else: jpayne@68: the_module = sys.modules[start_dir] jpayne@68: top_part = start_dir.split('.')[0] jpayne@68: try: jpayne@68: start_dir = os.path.abspath( jpayne@68: os.path.dirname((the_module.__file__))) jpayne@68: except AttributeError: jpayne@68: # look for namespace packages jpayne@68: try: jpayne@68: spec = the_module.__spec__ jpayne@68: except AttributeError: jpayne@68: spec = None jpayne@68: jpayne@68: if spec and spec.loader is None: jpayne@68: if spec.submodule_search_locations is not None: jpayne@68: is_namespace = True jpayne@68: jpayne@68: for path in the_module.__path__: jpayne@68: if (not set_implicit_top and jpayne@68: not path.startswith(top_level_dir)): jpayne@68: continue jpayne@68: self._top_level_dir = \ jpayne@68: (path.split(the_module.__name__ jpayne@68: .replace(".", os.path.sep))[0]) jpayne@68: tests.extend(self._find_tests(path, jpayne@68: pattern, jpayne@68: namespace=True)) jpayne@68: elif the_module.__name__ in sys.builtin_module_names: jpayne@68: # builtin module jpayne@68: raise TypeError('Can not use builtin modules ' jpayne@68: 'as dotted module names') from None jpayne@68: else: jpayne@68: raise TypeError( jpayne@68: 'don\'t know how to discover from {!r}' jpayne@68: .format(the_module)) from None jpayne@68: jpayne@68: if set_implicit_top: jpayne@68: if not is_namespace: jpayne@68: self._top_level_dir = \ jpayne@68: self._get_directory_containing_module(top_part) jpayne@68: sys.path.remove(top_level_dir) jpayne@68: else: jpayne@68: sys.path.remove(top_level_dir) jpayne@68: jpayne@68: if is_not_importable: jpayne@68: raise ImportError('Start directory is not importable: %r' % start_dir) jpayne@68: jpayne@68: if not is_namespace: jpayne@68: tests = list(self._find_tests(start_dir, pattern)) jpayne@68: return self.suiteClass(tests) jpayne@68: jpayne@68: def _get_directory_containing_module(self, module_name): jpayne@68: module = sys.modules[module_name] jpayne@68: full_path = os.path.abspath(module.__file__) jpayne@68: jpayne@68: if os.path.basename(full_path).lower().startswith('__init__.py'): jpayne@68: return os.path.dirname(os.path.dirname(full_path)) jpayne@68: else: jpayne@68: # here we have been given a module rather than a package - so jpayne@68: # all we can do is search the *same* directory the module is in jpayne@68: # should an exception be raised instead jpayne@68: return os.path.dirname(full_path) jpayne@68: jpayne@68: def _get_name_from_path(self, path): jpayne@68: if path == self._top_level_dir: jpayne@68: return '.' jpayne@68: path = _jython_aware_splitext(os.path.normpath(path)) jpayne@68: jpayne@68: _relpath = os.path.relpath(path, self._top_level_dir) jpayne@68: assert not os.path.isabs(_relpath), "Path must be within the project" jpayne@68: assert not _relpath.startswith('..'), "Path must be within the project" jpayne@68: jpayne@68: name = _relpath.replace(os.path.sep, '.') jpayne@68: return name jpayne@68: jpayne@68: def _get_module_from_name(self, name): jpayne@68: __import__(name) jpayne@68: return sys.modules[name] jpayne@68: jpayne@68: def _match_path(self, path, full_path, pattern): jpayne@68: # override this method to use alternative matching strategy jpayne@68: return fnmatch(path, pattern) jpayne@68: jpayne@68: def _find_tests(self, start_dir, pattern, namespace=False): jpayne@68: """Used by discovery. Yields test suites it loads.""" jpayne@68: # Handle the __init__ in this package jpayne@68: name = self._get_name_from_path(start_dir) jpayne@68: # name is '.' when start_dir == top_level_dir (and top_level_dir is by jpayne@68: # definition not a package). jpayne@68: if name != '.' and name not in self._loading_packages: jpayne@68: # name is in self._loading_packages while we have called into jpayne@68: # loadTestsFromModule with name. jpayne@68: tests, should_recurse = self._find_test_path( jpayne@68: start_dir, pattern, namespace) jpayne@68: if tests is not None: jpayne@68: yield tests jpayne@68: if not should_recurse: jpayne@68: # Either an error occurred, or load_tests was used by the jpayne@68: # package. jpayne@68: return jpayne@68: # Handle the contents. jpayne@68: paths = sorted(os.listdir(start_dir)) jpayne@68: for path in paths: jpayne@68: full_path = os.path.join(start_dir, path) jpayne@68: tests, should_recurse = self._find_test_path( jpayne@68: full_path, pattern, namespace) jpayne@68: if tests is not None: jpayne@68: yield tests jpayne@68: if should_recurse: jpayne@68: # we found a package that didn't use load_tests. jpayne@68: name = self._get_name_from_path(full_path) jpayne@68: self._loading_packages.add(name) jpayne@68: try: jpayne@68: yield from self._find_tests(full_path, pattern, namespace) jpayne@68: finally: jpayne@68: self._loading_packages.discard(name) jpayne@68: jpayne@68: def _find_test_path(self, full_path, pattern, namespace=False): jpayne@68: """Used by discovery. jpayne@68: jpayne@68: Loads tests from a single file, or a directories' __init__.py when jpayne@68: passed the directory. jpayne@68: jpayne@68: Returns a tuple (None_or_tests_from_file, should_recurse). jpayne@68: """ jpayne@68: basename = os.path.basename(full_path) jpayne@68: if os.path.isfile(full_path): jpayne@68: if not VALID_MODULE_NAME.match(basename): jpayne@68: # valid Python identifiers only jpayne@68: return None, False jpayne@68: if not self._match_path(basename, full_path, pattern): jpayne@68: return None, False jpayne@68: # if the test file matches, load it jpayne@68: name = self._get_name_from_path(full_path) jpayne@68: try: jpayne@68: module = self._get_module_from_name(name) jpayne@68: except case.SkipTest as e: jpayne@68: return _make_skipped_test(name, e, self.suiteClass), False jpayne@68: except: jpayne@68: error_case, error_message = \ jpayne@68: _make_failed_import_test(name, self.suiteClass) jpayne@68: self.errors.append(error_message) jpayne@68: return error_case, False jpayne@68: else: jpayne@68: mod_file = os.path.abspath( jpayne@68: getattr(module, '__file__', full_path)) jpayne@68: realpath = _jython_aware_splitext( jpayne@68: os.path.realpath(mod_file)) jpayne@68: fullpath_noext = _jython_aware_splitext( jpayne@68: os.path.realpath(full_path)) jpayne@68: if realpath.lower() != fullpath_noext.lower(): jpayne@68: module_dir = os.path.dirname(realpath) jpayne@68: mod_name = _jython_aware_splitext( jpayne@68: os.path.basename(full_path)) jpayne@68: expected_dir = os.path.dirname(full_path) jpayne@68: msg = ("%r module incorrectly imported from %r. Expected " jpayne@68: "%r. Is this module globally installed?") jpayne@68: raise ImportError( jpayne@68: msg % (mod_name, module_dir, expected_dir)) jpayne@68: return self.loadTestsFromModule(module, pattern=pattern), False jpayne@68: elif os.path.isdir(full_path): jpayne@68: if (not namespace and jpayne@68: not os.path.isfile(os.path.join(full_path, '__init__.py'))): jpayne@68: return None, False jpayne@68: jpayne@68: load_tests = None jpayne@68: tests = None jpayne@68: name = self._get_name_from_path(full_path) jpayne@68: try: jpayne@68: package = self._get_module_from_name(name) jpayne@68: except case.SkipTest as e: jpayne@68: return _make_skipped_test(name, e, self.suiteClass), False jpayne@68: except: jpayne@68: error_case, error_message = \ jpayne@68: _make_failed_import_test(name, self.suiteClass) jpayne@68: self.errors.append(error_message) jpayne@68: return error_case, False jpayne@68: else: jpayne@68: load_tests = getattr(package, 'load_tests', None) jpayne@68: # Mark this package as being in load_tests (possibly ;)) jpayne@68: self._loading_packages.add(name) jpayne@68: try: jpayne@68: tests = self.loadTestsFromModule(package, pattern=pattern) jpayne@68: if load_tests is not None: jpayne@68: # loadTestsFromModule(package) has loaded tests for us. jpayne@68: return tests, False jpayne@68: return tests, True jpayne@68: finally: jpayne@68: self._loading_packages.discard(name) jpayne@68: else: jpayne@68: return None, False jpayne@68: jpayne@68: jpayne@68: defaultTestLoader = TestLoader() jpayne@68: jpayne@68: jpayne@68: def _makeLoader(prefix, sortUsing, suiteClass=None, testNamePatterns=None): jpayne@68: loader = TestLoader() jpayne@68: loader.sortTestMethodsUsing = sortUsing jpayne@68: loader.testMethodPrefix = prefix jpayne@68: loader.testNamePatterns = testNamePatterns jpayne@68: if suiteClass: jpayne@68: loader.suiteClass = suiteClass jpayne@68: return loader jpayne@68: jpayne@68: def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp, testNamePatterns=None): jpayne@68: return _makeLoader(prefix, sortUsing, testNamePatterns=testNamePatterns).getTestCaseNames(testCaseClass) jpayne@68: jpayne@68: def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp, jpayne@68: suiteClass=suite.TestSuite): jpayne@68: return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase( jpayne@68: testCaseClass) jpayne@68: jpayne@68: def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp, jpayne@68: suiteClass=suite.TestSuite): jpayne@68: return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\ jpayne@68: module)