2010-11-18 05:12:16 +08:00
|
|
|
import pytest, py, os
|
2010-12-06 23:54:42 +08:00
|
|
|
from _pytest.core import PluginManager
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import MultiCall, HookRelay, varnames
|
2013-09-30 19:14:14 +08:00
|
|
|
from _pytest.config import get_plugin_manager
|
2009-12-29 19:36:17 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestBootstrapping:
|
|
|
|
def test_consider_env_fails_to_import(self, monkeypatch):
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager = PluginManager()
|
2009-08-14 02:10:12 +08:00
|
|
|
monkeypatch.setenv('PYTEST_PLUGINS', 'nonexisting', prepend=",")
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ImportError, lambda: pluginmanager.consider_env())
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-09 01:18:26 +08:00
|
|
|
def test_preparse_args(self):
|
2009-04-18 02:09:29 +08:00
|
|
|
pluginmanager = PluginManager()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ImportError, lambda:
|
|
|
|
pluginmanager.consider_preparse(["xyz", "-p", "hello123"]))
|
2009-04-18 02:09:29 +08:00
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
def test_plugin_prevent_register(self):
|
|
|
|
pluginmanager = PluginManager()
|
|
|
|
pluginmanager.consider_preparse(["xyz", "-p", "no:abc"])
|
|
|
|
l1 = pluginmanager.getplugins()
|
|
|
|
pluginmanager.register(42, name="abc")
|
|
|
|
l2 = pluginmanager.getplugins()
|
|
|
|
assert len(l2) == len(l1)
|
|
|
|
|
|
|
|
def test_plugin_prevent_register_unregistered_alredy_registered(self):
|
|
|
|
pluginmanager = PluginManager()
|
|
|
|
pluginmanager.register(42, name="abc")
|
|
|
|
l1 = pluginmanager.getplugins()
|
|
|
|
assert 42 in l1
|
|
|
|
pluginmanager.consider_preparse(["xyz", "-p", "no:abc"])
|
|
|
|
l2 = pluginmanager.getplugins()
|
|
|
|
assert 42 not in l2
|
|
|
|
|
2012-06-17 03:29:04 +08:00
|
|
|
def test_plugin_double_register(self):
|
|
|
|
pm = PluginManager()
|
|
|
|
pm.register(42, name="abc")
|
|
|
|
pytest.raises(ValueError, lambda: pm.register(42, name="abc"))
|
|
|
|
|
2009-07-09 01:18:26 +08:00
|
|
|
def test_plugin_skip(self, testdir, monkeypatch):
|
2010-12-06 23:54:42 +08:00
|
|
|
p = testdir.makepyfile(skipping1="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytest.skip("hello")
|
2009-07-09 01:18:26 +08:00
|
|
|
""")
|
2010-12-06 23:54:42 +08:00
|
|
|
p.copy(p.dirpath("skipping2.py"))
|
2010-01-03 05:48:53 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "skipping2")
|
|
|
|
result = testdir.runpytest("-p", "skipping1", "--traceconfig")
|
|
|
|
assert result.ret == 0
|
2009-07-09 01:18:26 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2010-01-03 05:48:53 +08:00
|
|
|
"*hint*skipping1*hello*",
|
2010-12-06 23:54:42 +08:00
|
|
|
"*hint*skipping2*hello*",
|
2009-07-09 01:18:26 +08:00
|
|
|
])
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_consider_env_plugin_instantiation(self, testdir, monkeypatch):
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager = PluginManager()
|
2009-02-27 18:18:27 +08:00
|
|
|
testdir.syspathinsert()
|
2010-12-06 23:54:42 +08:00
|
|
|
testdir.makepyfile(xy123="#")
|
2009-02-27 18:18:27 +08:00
|
|
|
monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'xy123')
|
2009-04-09 23:03:58 +08:00
|
|
|
l1 = len(pluginmanager.getplugins())
|
|
|
|
pluginmanager.consider_env()
|
|
|
|
l2 = len(pluginmanager.getplugins())
|
2010-07-27 03:15:15 +08:00
|
|
|
assert l2 == l1 + 1
|
2010-12-06 23:54:42 +08:00
|
|
|
assert pluginmanager.getplugin('xy123')
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager.consider_env()
|
|
|
|
l3 = len(pluginmanager.getplugins())
|
2009-02-27 18:18:27 +08:00
|
|
|
assert l2 == l3
|
|
|
|
|
2009-11-24 00:20:36 +08:00
|
|
|
def test_consider_setuptools_instantiation(self, monkeypatch):
|
|
|
|
pkg_resources = py.test.importorskip("pkg_resources")
|
|
|
|
def my_iter(name):
|
|
|
|
assert name == "pytest11"
|
|
|
|
class EntryPoint:
|
2010-12-06 23:54:42 +08:00
|
|
|
name = "pytest_mytestplugin"
|
2011-01-13 02:39:36 +08:00
|
|
|
dist = None
|
2009-11-24 00:20:36 +08:00
|
|
|
def load(self):
|
|
|
|
class PseudoPlugin:
|
|
|
|
x = 42
|
|
|
|
return PseudoPlugin()
|
|
|
|
return iter([EntryPoint()])
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-11-24 00:20:36 +08:00
|
|
|
monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter)
|
|
|
|
pluginmanager = PluginManager()
|
|
|
|
pluginmanager.consider_setuptools_entrypoints()
|
|
|
|
plugin = pluginmanager.getplugin("mytestplugin")
|
|
|
|
assert plugin.x == 42
|
|
|
|
|
2009-11-24 17:49:04 +08:00
|
|
|
def test_consider_setuptools_not_installed(self, monkeypatch):
|
2010-07-27 03:15:15 +08:00
|
|
|
monkeypatch.setitem(py.std.sys.modules, 'pkg_resources',
|
2009-11-24 17:49:04 +08:00
|
|
|
py.std.types.ModuleType("pkg_resources"))
|
|
|
|
pluginmanager = PluginManager()
|
|
|
|
pluginmanager.consider_setuptools_entrypoints()
|
|
|
|
# ok, we did not explode
|
|
|
|
|
2009-04-09 22:21:07 +08:00
|
|
|
def test_pluginmanager_ENV_startup(self, testdir, monkeypatch):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile(pytest_x500="#")
|
2009-02-27 18:18:27 +08:00
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_hello(pytestconfig):
|
2010-12-06 23:54:42 +08:00
|
|
|
plugin = pytestconfig.pluginmanager.getplugin('pytest_x500')
|
2009-02-27 18:18:27 +08:00
|
|
|
assert plugin is not None
|
|
|
|
""")
|
2009-08-14 02:10:12 +08:00
|
|
|
monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",")
|
2009-02-27 18:18:27 +08:00
|
|
|
result = testdir.runpytest(p)
|
|
|
|
assert result.ret == 0
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed in*"])
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_import_plugin_importname(self, testdir):
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager = PluginManager()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ImportError, 'pluginmanager.import_plugin("qweqwex.y")')
|
|
|
|
pytest.raises(ImportError, 'pluginmanager.import_plugin("pytest_qweqwx.y")')
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.syspathinsert()
|
2009-02-27 18:18:27 +08:00
|
|
|
pluginname = "pytest_hello"
|
2009-05-19 05:26:16 +08:00
|
|
|
testdir.makepyfile(**{pluginname: ""})
|
2010-12-06 23:54:42 +08:00
|
|
|
pluginmanager.import_plugin("pytest_hello")
|
2009-04-09 23:03:58 +08:00
|
|
|
len1 = len(pluginmanager.getplugins())
|
|
|
|
pluginmanager.import_plugin("pytest_hello")
|
|
|
|
len2 = len(pluginmanager.getplugins())
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len1 == len2
|
2009-04-09 23:03:58 +08:00
|
|
|
plugin1 = pluginmanager.getplugin("pytest_hello")
|
2009-05-19 05:26:16 +08:00
|
|
|
assert plugin1.__name__.endswith('pytest_hello')
|
2010-12-06 23:54:42 +08:00
|
|
|
plugin2 = pluginmanager.getplugin("pytest_hello")
|
2009-02-27 18:18:27 +08:00
|
|
|
assert plugin2 is plugin1
|
|
|
|
|
2010-11-01 02:01:46 +08:00
|
|
|
def test_import_plugin_dotted_name(self, testdir):
|
|
|
|
pluginmanager = PluginManager()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ImportError, 'pluginmanager.import_plugin("qweqwex.y")')
|
|
|
|
pytest.raises(ImportError, 'pluginmanager.import_plugin("pytest_qweqwex.y")')
|
2010-11-01 02:01:46 +08:00
|
|
|
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.syspathinsert()
|
2010-11-01 02:01:46 +08:00
|
|
|
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
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_consider_module(self, testdir):
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager = PluginManager()
|
2009-02-27 18:18:27 +08:00
|
|
|
testdir.syspathinsert()
|
2010-12-06 23:54:42 +08:00
|
|
|
testdir.makepyfile(pytest_p1="#")
|
|
|
|
testdir.makepyfile(pytest_p2="#")
|
2009-08-30 04:14:18 +08:00
|
|
|
mod = py.std.types.ModuleType("temp")
|
2010-12-06 23:54:42 +08:00
|
|
|
mod.pytest_plugins = ["pytest_p1", "pytest_p2"]
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager.consider_module(mod)
|
2010-12-06 23:54:42 +08:00
|
|
|
assert pluginmanager.getplugin("pytest_p1").__name__ == "pytest_p1"
|
|
|
|
assert pluginmanager.getplugin("pytest_p2").__name__ == "pytest_p2"
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-09 07:33:48 +08:00
|
|
|
def test_consider_module_import_module(self, testdir):
|
2009-08-30 04:14:18 +08:00
|
|
|
mod = py.std.types.ModuleType("x")
|
2009-02-27 18:18:27 +08:00
|
|
|
mod.pytest_plugins = "pytest_a"
|
2009-05-19 05:26:16 +08:00
|
|
|
aplugin = testdir.makepyfile(pytest_a="#")
|
2013-09-30 19:14:14 +08:00
|
|
|
pluginmanager = get_plugin_manager()
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.getreportrecorder(pluginmanager)
|
2009-02-27 18:18:27 +08:00
|
|
|
#syspath.prepend(aplugin.dirpath())
|
|
|
|
py.std.sys.path.insert(0, str(aplugin.dirpath()))
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager.consider_module(mod)
|
2009-05-21 20:33:09 +08:00
|
|
|
call = reprec.getcall(pluginmanager.hook.pytest_plugin_registered.name)
|
2009-05-19 05:26:16 +08:00
|
|
|
assert call.plugin.__name__ == "pytest_a"
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
# check that it is not registered twice
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager.consider_module(mod)
|
2009-05-21 20:33:09 +08:00
|
|
|
l = reprec.getcalls("pytest_plugin_registered")
|
2009-04-07 18:48:57 +08:00
|
|
|
assert len(l) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_config_sets_conftesthandle_onimport(self, testdir):
|
|
|
|
config = testdir.parseconfig([])
|
2009-03-17 05:17:14 +08:00
|
|
|
assert config._conftest._onimport == config._onimportconftest
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_consider_conftest_deps(self, testdir):
|
|
|
|
mod = testdir.makepyfile("pytest_plugins='xyz'").pyimport()
|
2009-04-09 22:03:09 +08:00
|
|
|
pp = PluginManager()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ImportError, lambda: pp.consider_conftest(mod))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def test_pm(self):
|
2009-04-09 22:03:09 +08:00
|
|
|
pp = PluginManager()
|
2010-07-27 03:15:15 +08:00
|
|
|
class A: pass
|
2009-05-23 05:50:35 +08:00
|
|
|
a1, a2 = A(), A()
|
2009-02-27 18:18:27 +08:00
|
|
|
pp.register(a1)
|
|
|
|
assert pp.isregistered(a1)
|
2009-10-17 18:56:59 +08:00
|
|
|
pp.register(a2, "hello")
|
2009-02-27 18:18:27 +08:00
|
|
|
assert pp.isregistered(a2)
|
2009-12-29 17:26:51 +08:00
|
|
|
l = pp.getplugins()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert a1 in l
|
2009-12-29 17:26:51 +08:00
|
|
|
assert a2 in l
|
2009-10-17 18:56:59 +08:00
|
|
|
assert pp.getplugin('hello') == a2
|
2009-02-27 18:18:27 +08:00
|
|
|
pp.unregister(a1)
|
|
|
|
assert not pp.isregistered(a1)
|
2010-11-01 16:20:58 +08:00
|
|
|
pp.unregister(name="hello")
|
2009-02-27 18:18:27 +08:00
|
|
|
assert not pp.isregistered(a2)
|
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def test_pm_ordering(self):
|
2010-09-26 22:23:43 +08:00
|
|
|
pp = PluginManager()
|
|
|
|
class A: pass
|
|
|
|
a1, a2 = A(), A()
|
|
|
|
pp.register(a1)
|
|
|
|
pp.register(a2, "hello")
|
|
|
|
l = pp.getplugins()
|
|
|
|
assert l.index(a1) < l.index(a2)
|
|
|
|
a3 = A()
|
|
|
|
pp.register(a3, prepend=True)
|
|
|
|
l = pp.getplugins()
|
|
|
|
assert l.index(a3) == 0
|
|
|
|
|
2009-05-20 01:25:21 +08:00
|
|
|
def test_register_imported_modules(self):
|
|
|
|
pp = PluginManager()
|
2009-08-30 04:14:18 +08:00
|
|
|
mod = py.std.types.ModuleType("x.y.pytest_hello")
|
2009-05-20 01:25:21 +08:00
|
|
|
pp.register(mod)
|
|
|
|
assert pp.isregistered(mod)
|
2009-12-29 17:26:51 +08:00
|
|
|
l = pp.getplugins()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert mod in l
|
2012-06-17 03:29:04 +08:00
|
|
|
pytest.raises(ValueError, "pp.register(mod)")
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ValueError, lambda: pp.register(mod))
|
2009-05-20 01:25:21 +08:00
|
|
|
#assert not pp.isregistered(mod2)
|
2009-12-29 17:26:51 +08:00
|
|
|
assert pp.getplugins() == l
|
2009-05-20 01:25:21 +08:00
|
|
|
|
2009-10-17 18:56:59 +08:00
|
|
|
def test_canonical_import(self, monkeypatch):
|
|
|
|
mod = py.std.types.ModuleType("pytest_xyz")
|
|
|
|
monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod)
|
|
|
|
pp = PluginManager()
|
2010-12-06 23:54:42 +08:00
|
|
|
pp.import_plugin('pytest_xyz')
|
2009-10-17 18:56:59 +08:00
|
|
|
assert pp.getplugin('pytest_xyz') == mod
|
|
|
|
assert pp.isregistered(mod)
|
|
|
|
|
2009-05-23 05:50:35 +08:00
|
|
|
def test_register_mismatch_method(self):
|
2013-09-30 19:14:14 +08:00
|
|
|
pp = get_plugin_manager()
|
2009-05-23 05:50:35 +08:00
|
|
|
class hello:
|
|
|
|
def pytest_gurgel(self):
|
|
|
|
pass
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(Exception, lambda: pp.register(hello()))
|
2009-05-23 05:50:35 +08:00
|
|
|
|
|
|
|
def test_register_mismatch_arg(self):
|
2013-09-30 19:14:14 +08:00
|
|
|
pp = get_plugin_manager()
|
2009-05-23 05:50:35 +08:00
|
|
|
class hello:
|
|
|
|
def pytest_configure(self, asd):
|
|
|
|
pass
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(Exception, lambda: pp.register(hello()))
|
2009-05-23 05:50:35 +08:00
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def test_register(self):
|
2013-09-30 19:14:14 +08:00
|
|
|
pm = get_plugin_manager()
|
2010-10-13 17:12:27 +08:00
|
|
|
class MyPlugin:
|
|
|
|
pass
|
|
|
|
my = MyPlugin()
|
|
|
|
pm.register(my)
|
|
|
|
assert pm.getplugins()
|
|
|
|
my2 = MyPlugin()
|
|
|
|
pm.register(my2)
|
2013-09-30 19:14:14 +08:00
|
|
|
assert pm.getplugins()[-2:] == [my, my2]
|
2010-10-13 17:12:27 +08:00
|
|
|
|
|
|
|
assert pm.isregistered(my)
|
|
|
|
assert pm.isregistered(my2)
|
|
|
|
pm.unregister(my)
|
|
|
|
assert not pm.isregistered(my)
|
2013-09-30 19:14:14 +08:00
|
|
|
assert pm.getplugins()[-1:] == [my2]
|
2010-10-13 17:12:27 +08:00
|
|
|
|
|
|
|
def test_listattr(self):
|
|
|
|
plugins = PluginManager()
|
|
|
|
class api1:
|
|
|
|
x = 41
|
|
|
|
class api2:
|
|
|
|
x = 42
|
|
|
|
class api3:
|
|
|
|
x = 43
|
|
|
|
plugins.register(api1())
|
|
|
|
plugins.register(api2())
|
|
|
|
plugins.register(api3())
|
|
|
|
l = list(plugins.listattr('x'))
|
|
|
|
assert l == [41, 42, 43]
|
2010-10-05 23:21:41 +08:00
|
|
|
|
2010-11-07 03:06:32 +08:00
|
|
|
def test_hook_tracing(self):
|
2013-09-30 19:14:14 +08:00
|
|
|
pm = get_plugin_manager()
|
2010-11-07 03:06:32 +08:00
|
|
|
saveindent = []
|
2010-11-06 06:37:31 +08:00
|
|
|
class api1:
|
|
|
|
x = 41
|
2010-11-07 03:06:32 +08:00
|
|
|
def pytest_plugin_registered(self, plugin):
|
|
|
|
saveindent.append(pm.trace.root.indent)
|
|
|
|
raise ValueError(42)
|
2010-11-06 06:37:31 +08:00
|
|
|
l = []
|
2010-11-07 03:06:32 +08:00
|
|
|
pm.trace.root.setwriter(l.append)
|
|
|
|
indent = pm.trace.root.indent
|
2010-11-06 06:37:31 +08:00
|
|
|
p = api1()
|
|
|
|
pm.register(p)
|
2010-11-07 03:06:32 +08:00
|
|
|
|
|
|
|
assert pm.trace.root.indent == indent
|
2010-11-06 06:37:31 +08:00
|
|
|
assert len(l) == 1
|
2010-11-07 03:06:32 +08:00
|
|
|
assert 'pytest_plugin_registered' in l[0]
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ValueError, lambda: pm.register(api1()))
|
2010-11-07 03:06:32 +08:00
|
|
|
assert pm.trace.root.indent == indent
|
|
|
|
assert saveindent[0] > indent
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestPytestPluginInteractions:
|
2010-05-02 22:36:53 +08:00
|
|
|
|
|
|
|
def test_addhooks_conftestplugin(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile(newhooks="""
|
2010-05-02 22:36:53 +08:00
|
|
|
def pytest_myhook(xyz):
|
|
|
|
"new hook"
|
|
|
|
""")
|
|
|
|
conf = testdir.makeconftest("""
|
|
|
|
import sys ; sys.path.insert(0, '.')
|
|
|
|
import newhooks
|
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
pluginmanager.addhooks(newhooks)
|
|
|
|
def pytest_myhook(xyz):
|
|
|
|
return xyz + 1
|
|
|
|
""")
|
2013-09-30 19:14:14 +08:00
|
|
|
config = get_plugin_manager().config
|
2010-05-02 22:36:53 +08:00
|
|
|
config._conftest.importconftest(conf)
|
|
|
|
print(config.pluginmanager.getplugins())
|
|
|
|
res = config.hook.pytest_myhook(xyz=10)
|
|
|
|
assert res == [11]
|
|
|
|
|
|
|
|
def test_addhooks_nohooks(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeconftest("""
|
2010-07-27 03:15:15 +08:00
|
|
|
import sys
|
2010-05-02 22:36:53 +08:00
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
pluginmanager.addhooks(sys)
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest()
|
|
|
|
assert res.ret != 0
|
|
|
|
res.stderr.fnmatch_lines([
|
|
|
|
"*did not find*sys*"
|
|
|
|
])
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def test_namespace_early_from_import(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-13 16:05:11 +08:00
|
|
|
from pytest import Item
|
|
|
|
from pytest import Item as Item2
|
2010-10-12 18:19:53 +08:00
|
|
|
assert Item is Item2
|
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2009-06-18 23:19:12 +08:00
|
|
|
def test_do_ext_namespace(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2009-07-18 00:07:37 +08:00
|
|
|
def pytest_namespace():
|
2009-06-18 23:19:12 +08:00
|
|
|
return {'hello': 'world'}
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("""
|
2010-07-27 03:15:15 +08:00
|
|
|
from py.test import hello
|
2009-06-18 23:19:12 +08:00
|
|
|
import py
|
|
|
|
def test_hello():
|
2010-07-27 03:15:15 +08:00
|
|
|
assert hello == "world"
|
2009-12-29 23:29:48 +08:00
|
|
|
assert 'hello' in py.test.__all__
|
2009-06-18 23:19:12 +08:00
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
result = testdir.runpytest(p)
|
2009-08-10 05:46:27 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-06-18 23:19:12 +08:00
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_do_option_postinitialize(self, testdir):
|
2011-11-08 02:08:41 +08:00
|
|
|
config = testdir.parseconfigure()
|
2009-02-27 18:18:27 +08:00
|
|
|
assert not hasattr(config.option, 'test123')
|
|
|
|
p = testdir.makepyfile("""
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-07-27 03:15:15 +08:00
|
|
|
parser.addoption('--test123', action="store_true",
|
2009-05-19 05:26:16 +08:00
|
|
|
default=True)
|
2009-02-27 18:18:27 +08:00
|
|
|
""")
|
|
|
|
config._conftest.importconftest(p)
|
|
|
|
assert config.option.test123
|
|
|
|
|
|
|
|
def test_configure(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
l = []
|
|
|
|
class A:
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
l.append(self)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.register(A())
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(l) == 0
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_configure()
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(l) == 1
|
2010-12-06 23:54:42 +08:00
|
|
|
config.pluginmanager.register(A()) # leads to a configured() plugin
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(l) == 2
|
|
|
|
assert l[0] != l[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_unconfigure()
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.register(A())
|
2009-02-27 18:18:27 +08:00
|
|
|
assert len(l) == 2
|
|
|
|
|
|
|
|
# lower level API
|
|
|
|
|
|
|
|
def test_listattr(self):
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager = PluginManager()
|
2009-02-27 18:18:27 +08:00
|
|
|
class My2:
|
|
|
|
x = 42
|
2009-04-09 23:03:58 +08:00
|
|
|
pluginmanager.register(My2())
|
|
|
|
assert not pluginmanager.listattr("hello")
|
|
|
|
assert pluginmanager.listattr("x") == [42]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
def test_listattr_tryfirst(self):
|
|
|
|
class P1:
|
|
|
|
@pytest.mark.tryfirst
|
|
|
|
def m(self):
|
|
|
|
return 17
|
|
|
|
|
|
|
|
class P2:
|
|
|
|
def m(self):
|
|
|
|
return 23
|
|
|
|
class P3:
|
|
|
|
def m(self):
|
|
|
|
return 19
|
|
|
|
|
|
|
|
pluginmanager = PluginManager()
|
|
|
|
p1 = P1()
|
|
|
|
p2 = P2()
|
|
|
|
p3 = P3()
|
|
|
|
pluginmanager.register(p1)
|
|
|
|
pluginmanager.register(p2)
|
|
|
|
pluginmanager.register(p3)
|
|
|
|
methods = pluginmanager.listattr('m')
|
|
|
|
assert methods == [p2.m, p3.m, p1.m]
|
2011-03-08 01:28:45 +08:00
|
|
|
# listattr keeps a cache and deleting
|
|
|
|
# a function attribute requires clearing it
|
|
|
|
pluginmanager._listattrcache.clear()
|
2010-11-22 06:17:59 +08:00
|
|
|
del P1.m.__dict__['tryfirst']
|
|
|
|
|
|
|
|
pytest.mark.trylast(getattr(P2.m, 'im_func', P2.m))
|
|
|
|
methods = pluginmanager.listattr('m')
|
|
|
|
assert methods == [p2.m, p1.m, p3.m]
|
|
|
|
|
|
|
|
|
2009-08-10 05:46:27 +08:00
|
|
|
def test_namespace_has_default_and_env_plugins(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytest.mark
|
2009-08-10 05:46:27 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
assert result.ret == 0
|
2009-12-29 19:36:17 +08:00
|
|
|
|
|
|
|
def test_varnames():
|
|
|
|
def f(x):
|
2013-10-12 21:39:22 +08:00
|
|
|
i = 3 # noqa
|
2009-12-29 19:36:17 +08:00
|
|
|
class A:
|
|
|
|
def f(self, y):
|
|
|
|
pass
|
2010-09-07 16:03:11 +08:00
|
|
|
class B(object):
|
|
|
|
def __call__(self, z):
|
|
|
|
pass
|
2009-12-29 19:36:17 +08:00
|
|
|
assert varnames(f) == ("x",)
|
|
|
|
assert varnames(A().f) == ('y',)
|
2010-09-07 16:03:11 +08:00
|
|
|
assert varnames(B()) == ('z',)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-12-29 19:36:17 +08:00
|
|
|
class TestMultiCall:
|
|
|
|
def test_uses_copy_of_methods(self):
|
|
|
|
l = [lambda: 42]
|
|
|
|
mc = MultiCall(l, {})
|
|
|
|
repr(mc)
|
|
|
|
l[:] = []
|
|
|
|
res = mc.execute()
|
|
|
|
return res == 42
|
|
|
|
|
|
|
|
def test_call_passing(self):
|
|
|
|
class P1:
|
|
|
|
def m(self, __multicall__, x):
|
|
|
|
assert len(__multicall__.results) == 1
|
|
|
|
assert not __multicall__.methods
|
|
|
|
return 17
|
|
|
|
|
|
|
|
class P2:
|
|
|
|
def m(self, __multicall__, x):
|
|
|
|
assert __multicall__.results == []
|
|
|
|
assert __multicall__.methods
|
2010-07-27 03:15:15 +08:00
|
|
|
return 23
|
|
|
|
|
|
|
|
p1 = P1()
|
|
|
|
p2 = P2()
|
2009-12-29 19:36:17 +08:00
|
|
|
multicall = MultiCall([p1.m, p2.m], {'x': 23})
|
|
|
|
assert "23" in repr(multicall)
|
|
|
|
reslist = multicall.execute()
|
|
|
|
assert len(reslist) == 2
|
2010-07-27 03:15:15 +08:00
|
|
|
# ensure reversed order
|
2009-12-29 19:36:17 +08:00
|
|
|
assert reslist == [23, 17]
|
|
|
|
|
|
|
|
def test_keyword_args(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
def f(x):
|
2009-12-29 19:36:17 +08:00
|
|
|
return x + 1
|
|
|
|
class A:
|
|
|
|
def f(self, x, y):
|
|
|
|
return x + y
|
|
|
|
multicall = MultiCall([f, A().f], dict(x=23, y=24))
|
|
|
|
assert "'x': 23" in repr(multicall)
|
|
|
|
assert "'y': 24" in repr(multicall)
|
|
|
|
reslist = multicall.execute()
|
|
|
|
assert reslist == [24+23, 24]
|
|
|
|
assert "2 results" in repr(multicall)
|
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def test_keyword_args_with_defaultargs(self):
|
|
|
|
def f(x, z=1):
|
|
|
|
return x + z
|
|
|
|
reslist = MultiCall([f], dict(x=23, y=24)).execute()
|
|
|
|
assert reslist == [24]
|
|
|
|
reslist = MultiCall([f], dict(x=23, z=2)).execute()
|
|
|
|
assert reslist == [25]
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_tags_call_error(self):
|
2009-12-29 19:36:17 +08:00
|
|
|
multicall = MultiCall([lambda x: x], {})
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(TypeError, multicall.execute)
|
2009-12-29 19:36:17 +08:00
|
|
|
|
|
|
|
def test_call_subexecute(self):
|
|
|
|
def m(__multicall__):
|
|
|
|
subresult = __multicall__.execute()
|
|
|
|
return subresult + 1
|
|
|
|
|
|
|
|
def n():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
call = MultiCall([n, m], {}, firstresult=True)
|
|
|
|
res = call.execute()
|
|
|
|
assert res == 2
|
|
|
|
|
|
|
|
def test_call_none_is_no_result(self):
|
|
|
|
def m1():
|
|
|
|
return 1
|
|
|
|
def m2():
|
|
|
|
return None
|
|
|
|
res = MultiCall([m1, m2], {}, firstresult=True).execute()
|
|
|
|
assert res == 1
|
|
|
|
res = MultiCall([m1, m2], {}).execute()
|
|
|
|
assert res == [1]
|
|
|
|
|
|
|
|
class TestHookRelay:
|
|
|
|
def test_happypath(self):
|
2010-10-13 17:12:27 +08:00
|
|
|
pm = PluginManager()
|
2009-12-29 19:36:17 +08:00
|
|
|
class Api:
|
|
|
|
def hello(self, arg):
|
2010-05-02 22:36:53 +08:00
|
|
|
"api hook 1"
|
2009-12-29 19:36:17 +08:00
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
mcm = HookRelay(hookspecs=Api, pm=pm, prefix="he")
|
2009-12-29 19:36:17 +08:00
|
|
|
assert hasattr(mcm, 'hello')
|
|
|
|
assert repr(mcm.hello).find("hello") != -1
|
|
|
|
class Plugin:
|
|
|
|
def hello(self, arg):
|
|
|
|
return arg + 1
|
2010-10-13 17:12:27 +08:00
|
|
|
pm.register(Plugin())
|
2009-12-29 19:36:17 +08:00
|
|
|
l = mcm.hello(arg=3)
|
|
|
|
assert l == [4]
|
|
|
|
assert not hasattr(mcm, 'world')
|
|
|
|
|
|
|
|
def test_only_kwargs(self):
|
2010-10-13 17:12:27 +08:00
|
|
|
pm = PluginManager()
|
2009-12-29 19:36:17 +08:00
|
|
|
class Api:
|
|
|
|
def hello(self, arg):
|
2010-05-02 22:36:53 +08:00
|
|
|
"api hook 1"
|
2010-10-13 17:12:27 +08:00
|
|
|
mcm = HookRelay(hookspecs=Api, pm=pm, prefix="he")
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(TypeError, lambda: mcm.hello(3))
|
2009-12-29 19:36:17 +08:00
|
|
|
|
|
|
|
def test_firstresult_definition(self):
|
2010-10-13 17:12:27 +08:00
|
|
|
pm = PluginManager()
|
2009-12-29 19:36:17 +08:00
|
|
|
class Api:
|
2010-07-27 03:15:15 +08:00
|
|
|
def hello(self, arg):
|
2010-05-02 22:36:53 +08:00
|
|
|
"api hook 1"
|
2009-12-29 19:36:17 +08:00
|
|
|
hello.firstresult = True
|
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
mcm = HookRelay(hookspecs=Api, pm=pm, prefix="he")
|
2009-12-29 19:36:17 +08:00
|
|
|
class Plugin:
|
|
|
|
def hello(self, arg):
|
|
|
|
return arg + 1
|
2010-10-13 17:12:27 +08:00
|
|
|
pm.register(Plugin())
|
2009-12-29 19:36:17 +08:00
|
|
|
res = mcm.hello(arg=3)
|
|
|
|
assert res == 4
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
class TestTracer:
|
|
|
|
def test_simple(self):
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import TagTracer
|
2011-07-15 01:11:50 +08:00
|
|
|
rootlogger = TagTracer()
|
2010-11-06 06:37:31 +08:00
|
|
|
log = rootlogger.get("pytest")
|
|
|
|
log("hello")
|
|
|
|
l = []
|
|
|
|
rootlogger.setwriter(l.append)
|
|
|
|
log("world")
|
|
|
|
assert len(l) == 1
|
2011-07-15 01:11:50 +08:00
|
|
|
assert l[0] == "world [pytest]\n"
|
2010-11-06 06:37:31 +08:00
|
|
|
sublog = log.get("collection")
|
|
|
|
sublog("hello")
|
2011-07-15 01:11:50 +08:00
|
|
|
assert l[1] == "hello [pytest:collection]\n"
|
2010-11-06 16:05:17 +08:00
|
|
|
|
|
|
|
def test_indent(self):
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import TagTracer
|
2010-11-06 16:05:17 +08:00
|
|
|
rootlogger = TagTracer()
|
|
|
|
log = rootlogger.get("1")
|
|
|
|
l = []
|
|
|
|
log.root.setwriter(lambda arg: l.append(arg))
|
|
|
|
log("hello")
|
|
|
|
log.root.indent += 1
|
|
|
|
log("line1")
|
|
|
|
log("line2")
|
|
|
|
log.root.indent += 1
|
|
|
|
log("line3")
|
|
|
|
log("line4")
|
|
|
|
log.root.indent -= 1
|
|
|
|
log("line5")
|
|
|
|
log.root.indent -= 1
|
|
|
|
log("last")
|
|
|
|
assert len(l) == 7
|
2011-07-15 01:11:50 +08:00
|
|
|
names = [x[:x.rfind(' [')] for x in l]
|
2010-11-06 16:05:17 +08:00
|
|
|
assert names == ['hello', ' line1', ' line2',
|
|
|
|
' line3', ' line4', ' line5', 'last']
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2012-11-29 17:04:39 +08:00
|
|
|
def test_readable_output_dictargs(self):
|
|
|
|
from _pytest.core import TagTracer
|
|
|
|
rootlogger = TagTracer()
|
|
|
|
|
|
|
|
out = rootlogger.format_message(['test'], [1])
|
|
|
|
assert out == ['1 [test]\n']
|
|
|
|
|
|
|
|
out2= rootlogger.format_message(['test'], ['test', {'a':1}])
|
|
|
|
assert out2 ==[
|
|
|
|
'test [test]\n',
|
|
|
|
' a: 1\n'
|
|
|
|
]
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_setprocessor(self):
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import TagTracer
|
2010-11-06 06:37:31 +08:00
|
|
|
rootlogger = TagTracer()
|
|
|
|
log = rootlogger.get("1")
|
|
|
|
log2 = log.get("2")
|
|
|
|
assert log2.tags == tuple("12")
|
|
|
|
l = []
|
|
|
|
rootlogger.setprocessor(tuple("12"), lambda *args: l.append(args))
|
|
|
|
log("not seen")
|
|
|
|
log2("seen")
|
|
|
|
assert len(l) == 1
|
|
|
|
tags, args = l[0]
|
|
|
|
assert "1" in tags
|
|
|
|
assert "2" in tags
|
|
|
|
assert args == ("seen",)
|
|
|
|
l2 = []
|
|
|
|
rootlogger.setprocessor("1:2", lambda *args: l2.append(args))
|
|
|
|
log2("seen")
|
|
|
|
tags, args = l2[0]
|
|
|
|
assert args == ("seen",)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
def test_setmyprocessor(self):
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import TagTracer
|
2010-11-06 06:37:31 +08:00
|
|
|
rootlogger = TagTracer()
|
|
|
|
log = rootlogger.get("1")
|
|
|
|
log2 = log.get("2")
|
|
|
|
l = []
|
|
|
|
log2.setmyprocessor(lambda *args: l.append(args))
|
|
|
|
log("not seen")
|
|
|
|
assert not l
|
|
|
|
log2(42)
|
|
|
|
assert len(l) == 1
|
|
|
|
tags, args = l[0]
|
|
|
|
assert "1" in tags
|
|
|
|
assert "2" in tags
|
|
|
|
assert args == (42,)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
|
|
|
def test_default_markers(testdir):
|
|
|
|
result = testdir.runpytest("--markers")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*tryfirst*first*",
|
|
|
|
"*trylast*last*",
|
|
|
|
])
|