add documented hookimpl_opts and hookspec_opts decorators
so that one doesn't have to use pytest.mark or function-attribute setting anymore --HG-- branch : more_plugin
This commit is contained in:
@@ -66,7 +66,7 @@ def check_open_files(config):
|
||||
error.append(error[0])
|
||||
raise AssertionError("\n".join(error))
|
||||
|
||||
@pytest.mark.trylast
|
||||
@pytest.hookimpl_opts(trylast=True)
|
||||
def pytest_runtest_teardown(item, __multicall__):
|
||||
item.config._basedir.chdir()
|
||||
if hasattr(item.config, '_openfiles'):
|
||||
|
||||
@@ -563,7 +563,7 @@ class TestConftestCustomization:
|
||||
b = testdir.mkdir("a").mkdir("b")
|
||||
b.join("conftest.py").write(py.code.Source("""
|
||||
import pytest
|
||||
@pytest.mark.hookwrapper
|
||||
@pytest.hookimpl_opts(hookwrapper=True)
|
||||
def pytest_pycollect_makeitem():
|
||||
outcome = yield
|
||||
if outcome.excinfo is None:
|
||||
|
||||
@@ -192,6 +192,53 @@ class TestAddMethodOrdering:
|
||||
assert hc.nonwrappers == [he_method1_middle]
|
||||
assert hc.wrappers == [he_method1, he_method3]
|
||||
|
||||
def test_hookspec_opts(self, pm):
|
||||
class HookSpec:
|
||||
@hookspec_opts()
|
||||
def he_myhook1(self, arg1):
|
||||
pass
|
||||
|
||||
@hookspec_opts(firstresult=True)
|
||||
def he_myhook2(self, arg1):
|
||||
pass
|
||||
|
||||
@hookspec_opts(firstresult=False)
|
||||
def he_myhook3(self, arg1):
|
||||
pass
|
||||
|
||||
pm.addhooks(HookSpec)
|
||||
assert not pm.hook.he_myhook1.firstresult
|
||||
assert pm.hook.he_myhook2.firstresult
|
||||
assert not pm.hook.he_myhook3.firstresult
|
||||
|
||||
|
||||
def test_hookimpl_opts(self):
|
||||
for name in ["hookwrapper", "optionalhook", "tryfirst", "trylast"]:
|
||||
for val in [True, False]:
|
||||
@hookimpl_opts(**{name: val})
|
||||
def he_myhook1(self, arg1):
|
||||
pass
|
||||
if val:
|
||||
assert getattr(he_myhook1, name)
|
||||
else:
|
||||
assert not hasattr(he_myhook1, name)
|
||||
|
||||
def test_decorator_functional(self, pm):
|
||||
class HookSpec:
|
||||
@hookspec_opts(firstresult=True)
|
||||
def he_myhook(self, arg1):
|
||||
""" add to arg1 """
|
||||
pm.addhooks(HookSpec)
|
||||
|
||||
class Plugin:
|
||||
@hookimpl_opts()
|
||||
def he_myhook(self, arg1):
|
||||
return arg1 + 1
|
||||
|
||||
pm.register(Plugin())
|
||||
results = pm.hook.he_myhook(arg1=17)
|
||||
assert results == 18
|
||||
|
||||
|
||||
class TestPytestPluginInteractions:
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ def test_hookvalidation_unknown(testdir):
|
||||
def test_hookvalidation_optional(testdir):
|
||||
testdir.makeconftest("""
|
||||
import pytest
|
||||
@pytest.mark.optionalhook
|
||||
@pytest.hookimpl_opts(optionalhook=True)
|
||||
def pytest_hello(xyz):
|
||||
pass
|
||||
""")
|
||||
|
||||
@@ -510,7 +510,7 @@ class TestKeywordSelection:
|
||||
""")
|
||||
testdir.makepyfile(conftest="""
|
||||
import pytest
|
||||
@pytest.mark.hookwrapper
|
||||
@pytest.hookimpl_opts(hookwrapper=True)
|
||||
def pytest_pycollect_makeitem(name):
|
||||
outcome = yield
|
||||
if name == "TestClass":
|
||||
|
||||
Reference in New Issue
Block a user