How to use the nose2.util.module_from_name function in nose2

To help you get started, we’ve selected a few nose2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nose-devs / nose2 / nose2 / plugins / doctests.py View on Github external
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)
        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)
github nose-devs / nose2 / nose2 / plugins / loader / loadtests.py View on Github external
def handleDir(self, event):
        """Run ``load_tests`` in packages.

        If a package itself matches the test file pattern, run
        ``load_tests`` in its :file:`__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, _package_path = 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
github nose-devs / nose2 / nose2 / plugins / loader / discovery.py View on Github external
# 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))
github nose-devs / nose2 / nose2 / session.py View on Github external
"""
        # plugins set directly
        if modules is None:
            modules = []
        if exclude is None:
            exclude = []
        # plugins mentioned in config file(s)
        cfg = self.unittest
        more_plugins = cfg.as_list('plugins', [])
        cfg_exclude = cfg.as_list('exclude-plugins', [])
        exclude.extend(cfg_exclude)
        exclude = set(exclude)
        all_ = (set(modules) | set(more_plugins)) - exclude
        log.debug("Loading plugin modules: %s", all_)
        for module in sorted(all_):
            self.loadPluginsFromModule(util.module_from_name(module))
        self.hooks.pluginsLoaded(events.PluginsLoadedEvent(self.plugins))