From 23f8d8bce77d58b6694283441d7001b7707e3b58 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 31 Oct 2010 19:01:46 +0100 Subject: [PATCH] allow modules/conftest files specify dotted import paths for loading plugins --- doc/customize.txt | 12 ++++++++---- pytest/_core.py | 7 ++++--- testing/test_pluginmanager.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/doc/customize.txt b/doc/customize.txt index 47b6433f9..65e810bde 100644 --- a/doc/customize.txt +++ b/doc/customize.txt @@ -128,15 +128,19 @@ py.test loads plugin modules at tool startup in the following way: * by recursively loading all plugins specified by the ``pytest_plugins`` variable in ``conftest.py`` files -Requiring/Loading plugins in a test module or plugin +Requiring/Loading plugins in a test module or conftest file ------------------------------------------------------------- -You can require plugins in a test module or a plugin like this:: +You can require plugins in a test module or a conftest file like this:: pytest_plugins = "name1", "name2", -When the test module or plugin is loaded the specified plugins -will be loaded. +When the test module or conftest plugin is loaded the specified plugins +will be loaded as well. You can also use dotted path like this:: + + pytest_plugins = "myapp.testsupport.myplugin" + +which will import the specified module as a py.test plugin. .. _`setuptools entry points`: .. _registered: diff --git a/pytest/_core.py b/pytest/_core.py index f955c7fb5..5b7141a64 100644 --- a/pytest/_core.py +++ b/pytest/_core.py @@ -218,15 +218,16 @@ class PluginManager(object): kwargs=kwargs, firstresult=True).execute() def canonical_importname(name): + if '.' in name: + return name name = name.lower() if not name.startswith(IMPORTPREFIX): name = IMPORTPREFIX + name return name def importplugin(importspec): - #print "importing", importspec try: - return __import__(importspec) + return __import__(importspec, None, None, '__doc__') except ImportError: e = py.std.sys.exc_info()[1] if str(e).find(importspec) == -1: @@ -241,7 +242,7 @@ def importplugin(importspec): if str(e).find(name) == -1: raise # show the original exception, not the failing internal one - return __import__(importspec) + return __import__(importspec, None, None, '__doc__') class MultiCall: diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 023b36eb1..7570795bd 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -101,6 +101,18 @@ class TestBootstrapping: plugin2 = pluginmanager.getplugin("hello") assert plugin2 is plugin1 + def test_import_plugin_dotted_name(self, testdir): + pluginmanager = PluginManager() + py.test.raises(ImportError, 'pluginmanager.import_plugin("x.y")') + py.test.raises(ImportError, 'pluginmanager.import_plugin("pytest_x.y")') + + reset = testdir.syspathinsert() + testdir.mkpydir("pkg").join("plug.py").write("x=3") + pluginname = "pkg.plug" + pluginmanager.import_plugin(pluginname) + mod = pluginmanager.getplugin("pkg.plug") + assert mod.x == 3 + def test_consider_module(self, testdir): pluginmanager = PluginManager() testdir.syspathinsert()