def test_canonical_import(self, monkeypatch): mod = py.std.types.ModuleType("pytest_xyz") monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod) pm = PytestPluginManager() pm.import_plugin('pytest_xyz') assert pm.get_plugin('pytest_xyz') == mod assert pm.is_registered(mod)
def _build_config_for_path(path): """ Builds and returns a basic test configuration rooted at the given path. Args: path (LocalPath): The path to the files under test. Returns: Config: The generated test configuration. """ # Find the root directory of the package containing our files. for rootdir in path.parts(reverse=True): if rootdir.join("setup.py").exists(): break else: rootdir = path # Initialize a base configuration as pytest would. pluginmanager = PytestPluginManager() for spec in default_plugins: pluginmanager.import_plugin(spec) config = Config(pluginmanager) # Ensure that pytest sets its root directory (``config.rootdir``) to the # given path. If we don't, then using this configuration from outside of # this path will confuse pytest. args = [rootdir] config.parse(args) return config
def test_import_plugin_dotted_name(self, pytester: Pytester, pytestpm: PytestPluginManager) -> None: pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y") pytester.syspathinsert() pytester.mkpydir("pkg").joinpath("plug.py").write_text("x=3") pluginname = "pkg.plug" pytestpm.import_plugin(pluginname) mod = pytestpm.get_plugin("pkg.plug") assert mod.x == 3
def test_import_plugin_importname(self, pytester: Pytester, pytestpm: PytestPluginManager) -> None: pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwx.y") pytester.syspathinsert() pluginname = "pytest_hello" pytester.makepyfile(**{pluginname: ""}) pytestpm.import_plugin("pytest_hello") len1 = len(pytestpm.get_plugins()) pytestpm.import_plugin("pytest_hello") len2 = len(pytestpm.get_plugins()) assert len1 == len2 plugin1 = pytestpm.get_plugin("pytest_hello") assert plugin1.__name__.endswith("pytest_hello") plugin2 = pytestpm.get_plugin("pytest_hello") assert plugin2 is plugin1
def test_importplugin_error_message(pytester: Pytester, pytestpm: PytestPluginManager) -> None: """Don't hide import errors when importing plugins and provide an easy to debug message. See #375 and #1998. """ pytester.syspathinsert(pytester.path) pytester.makepyfile(qwe="""\ def test_traceback(): raise ImportError('Not possible to import: ☺') test_traceback() """) with pytest.raises(ImportError) as excinfo: pytestpm.import_plugin("qwe") assert str(excinfo.value).endswith( 'Error importing plugin "qwe": Not possible to import: ☺') assert "in test_traceback" in str(excinfo.traceback[-1])