def _find_tests_in_dir(self, event, full_path, top_level): log.debug("find in dir %s (%s)", full_path, top_level) dirname = os.path.basename(full_path) pattern = self.session.testFilePattern evt = events.HandleFileEvent(event.loader, dirname, full_path, pattern, top_level) result = self.session.hooks.handleDir(evt) if evt.extraTests: for test in evt.extraTests: yield test if evt.handled: if result: yield result return evt = events.MatchPathEvent(dirname, full_path, pattern) result = self.session.hooks.matchDirPath(evt) if evt.handled and not result: return for path in os.listdir(full_path): entry_path = os.path.join(full_path, path) if os.path.isfile(entry_path): for test in self._find_tests_in_file(event, path, entry_path, top_level): yield test elif os.path.isdir(entry_path): if ('test' in path.lower() or util.ispackage(entry_path) or path in self.session.libDirs): for test in self._find_tests(event, entry_path, top_level): yield test
def _find_tests_in_file(self, event, filename, full_path, top_level, module_name=None): log.debug("find in file %s (%s)", full_path, top_level) pattern = self.session.testFilePattern loader = event.loader evt = events.HandleFileEvent(loader, filename, full_path, pattern, top_level) result = self.session.hooks.handleFile(evt) if evt.extraTests: yield loader.suiteClass(evt.extraTests) if evt.handled: if result: yield result return if not util.valid_module_name(filename): # valid Python identifiers only return evt = events.MatchPathEvent(filename, full_path, pattern) result = self.session.hooks.matchPath(evt) if evt.handled: if not result: return elif not self._match_path(filename, full_path, pattern): return if module_name is None: module_name, package_path = util.name_from_path(full_path) util.ensure_importable(package_path) try: module = util.module_from_name(module_name) except: yield loader.failedImport(module_name) else: mod_file = os.path.abspath(getattr(module, '__file__', full_path)) realpath = os.path.splitext(mod_file)[0] fullpath_noext = os.path.splitext(full_path)[0] if realpath.lower() != fullpath_noext.lower(): module_dir = os.path.dirname(realpath) mod_name = os.path.splitext(os.path.basename(full_path))[0] expected_dir = os.path.dirname(full_path) msg = ("%r module incorrectly imported from %r. " "Expected %r. Is this module globally installed?") raise ImportError(msg % (mod_name, module_dir, expected_dir)) yield loader.loadTestsFromModule(module)
def handle_dir(self, event, full_path, top_level): dirname = os.path.basename(full_path) pattern = self.session.testFilePattern evt = events.HandleFileEvent(event.loader, dirname, full_path, pattern, top_level) result = self.session.hooks.handleDir(evt) if evt.extraTests: for test in evt.extraTests: yield test if evt.handled: if result: yield result self.event_handled = True return evt = events.MatchPathEvent(dirname, full_path, pattern) result = self.session.hooks.matchDirPath(evt) if evt.handled and not result: self.event_handled = True