allow modules/conftest files specify dotted import paths for loading plugins

This commit is contained in:
holger krekel 2010-10-31 19:01:46 +01:00
parent 03924d205d
commit 23f8d8bce7
3 changed files with 24 additions and 7 deletions

View File

@ -128,15 +128,19 @@ py.test loads plugin modules at tool startup in the following way:
* by recursively loading all plugins specified by the * by recursively loading all plugins specified by the
``pytest_plugins`` variable in ``conftest.py`` files ``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", pytest_plugins = "name1", "name2",
When the test module or plugin is loaded the specified plugins When the test module or conftest plugin is loaded the specified plugins
will be loaded. 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`: .. _`setuptools entry points`:
.. _registered: .. _registered:

View File

@ -218,15 +218,16 @@ class PluginManager(object):
kwargs=kwargs, firstresult=True).execute() kwargs=kwargs, firstresult=True).execute()
def canonical_importname(name): def canonical_importname(name):
if '.' in name:
return name
name = name.lower() name = name.lower()
if not name.startswith(IMPORTPREFIX): if not name.startswith(IMPORTPREFIX):
name = IMPORTPREFIX + name name = IMPORTPREFIX + name
return name return name
def importplugin(importspec): def importplugin(importspec):
#print "importing", importspec
try: try:
return __import__(importspec) return __import__(importspec, None, None, '__doc__')
except ImportError: except ImportError:
e = py.std.sys.exc_info()[1] e = py.std.sys.exc_info()[1]
if str(e).find(importspec) == -1: if str(e).find(importspec) == -1:
@ -241,7 +242,7 @@ def importplugin(importspec):
if str(e).find(name) == -1: if str(e).find(name) == -1:
raise raise
# show the original exception, not the failing internal one # show the original exception, not the failing internal one
return __import__(importspec) return __import__(importspec, None, None, '__doc__')
class MultiCall: class MultiCall:

View File

@ -101,6 +101,18 @@ class TestBootstrapping:
plugin2 = pluginmanager.getplugin("hello") plugin2 = pluginmanager.getplugin("hello")
assert plugin2 is plugin1 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): def test_consider_module(self, testdir):
pluginmanager = PluginManager() pluginmanager = PluginManager()
testdir.syspathinsert() testdir.syspathinsert()