def handleDir(self, event): """Run load_tests in packages. If a package itself matches the test file pattern, run load_tests in its __init__.py, and stop default test discovery for that package. """ if self._loading: return if (self._match(event.name, event.pattern) and util.ispackage(event.path)): name = util.name_from_path(event.path) module = util.module_from_name(name) load_tests = getattr(module, 'load_tests', None) if not load_tests: return self._loading = True try: suite = event.loader.suiteClass() try: suite = load_tests(event.loader, suite, event.pattern) except Exception as exc: log.exception( "Failed to load tests from %s via load_tests", module) suite.addTest( event.loader.failedLoadTests(module.__name__, exc)) event.handled = True return suite finally: self._loading = False
def handleFile(self, event): """Load doctests from text files and modules""" path = event.path _root, ext = os.path.splitext(path) if ext in self.extensions: suite = doctest.DocFileTest(path, module_relative=False) event.extraTests.append(suite) return elif not util.valid_module_name(os.path.basename(path)): return name = util.name_from_path(path) try: module = util.module_from_name(name) except Exception: # XXX log warning here? return if hasattr(module, '__test__') and not module.__test__: return try: suite = doctest.DocTestSuite(module) except ValueError: # doctest, very annoyingly, raises ValueError when # a module has no tests. return event.extraTests.append(suite)
def test_name_from_path(self): test_module = support_file('scenario/tests_in_package/pkg1/test/test_things.py') test_package_path = support_file('scenario/tests_in_package') self.assertEqual( util.name_from_path(test_module), ('pkg1.test.test_things', test_package_path) )
def handleFile(self, event): """Load doctests from text files and modules""" path = event.path _root, ext = os.path.splitext(path) if ext in self.extensions: optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_UDIFF # +REPORT_ONLY_FIRST_FAILURE suite = doctest.DocFileTest(path, module_relative=False, optionflags=optionflags) event.extraTests.append(suite) return elif not util.valid_module_name(os.path.basename(path)): return name, package_path = util.name_from_path(path) util.ensure_importable(package_path) try: module = util.module_from_name(name) except Exception: # XXX log warning here? return if hasattr(module, '__test__') and not module.__test__: return try: suite = doctest.DocTestSuite(module) except ValueError: # with python <= 3.5, doctest, very annoyingly, raises ValueError # when a module has no tests. return event.extraTests.append(suite)
def handleFile(self, event): """Load doctests from text files and modules""" path = event.path _root, ext = os.path.splitext(path) if ext in self.extensions: suite = doctest.DocFileTest(path, module_relative=False) event.extraTests.append(suite) return elif not util.valid_module_name(os.path.basename(path)): return name, package_path = util.name_from_path(path) # ignore top-level setup.py which cannot be imported if name == "setup": return util.ensure_importable(package_path) try: module = util.module_from_name(name) except Exception: # XXX log warning here? return if hasattr(module, "__test__") and not module.__test__: return try: suite = doctest.DocTestSuite(module) except ValueError: # with python <= 3.5, doctest, very annoyingly, raises ValueError # when a module has no tests. return event.extraTests.append(suite)
def test_name_from_path(self): test_module = support_file( "scenario/tests_in_package/pkg1/test/test_things.py") test_package_path = support_file("scenario/tests_in_package") self.assertEqual( util.name_from_path(test_module), ("pkg1.test.test_things", test_package_path), )
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 _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 handleFile(self, event): """Produce a FeatureTestSuite from a .feature file.""" feature_path = event.path if os.path.splitext(feature_path)[1] != '.feature': return event.handled = True try: feature_package_name = name_from_path( os.path.dirname(feature_path))[0] feature_module = object_from_name(feature_package_name)[1] except: return event.loader.failedImport(feature_path) return self.makeSuiteFromFeature( module=feature_module, feature_path=feature_path, )
def handleFile(self, event): """Produce a FeatureTestSuite from a .feature file.""" feature_path = event.path if os.path.splitext(feature_path)[1] != '.feature': return event.handled = True try: feature_package_name = name_from_path( os.path.dirname(feature_path)) feature_module = object_from_name(feature_package_name)[1] except: return event.loader.failedImport(feature_path) return self.makeSuiteFromFeature( module=feature_module, feature_path=feature_path, )
def test_name_from_path(self): test_module = support_file( 'scenario/tests_in_package/pkg1/test/test_things.py') test_package_path = support_file('scenario/tests_in_package') self.assertEqual(util.name_from_path(test_module), ('pkg1.test.test_things', test_package_path))
def test_name_from_path(self): self.assertEqual( util.name_from_path(support_file('scenario/tests_in_package/pkg1/test/test_things.py')), 'pkg1.test.test_things')
def test_name_from_path(self): self.assertEqual( util.name_from_path( support_file( 'scenario/tests_in_package/pkg1/test/test_things.py')), 'pkg1.test.test_things')