How to access fixtures from pytest hook pytest_collection_modifyitems?

39 views Asked by At
$ ls
conftest.py  __pycache__  test.py
$ cat conftest.py 
import pytest

@pytest.fixture
def myfixture():
    t = 800
    yield t

def pytest_collection_modifyitems(config, items):
    # I want a reference to myfixture here
    pass


$ cat test.py 
import pytest

def test_trivial():
    assert True

Here is a minimum example of what I'm working with.

I've already tried the answers from Can pytest hooks use fixtures?

and they both don't work.

$ cat conftest.py 
import pytest

@pytest.fixture
def myfixture():
    t = 800
    yield t

def pytest_collection_modifyitems(config, items, request):
    f = request.getfixturevalue('myfixture')
    pass

$ pytest test.py 
Traceback (most recent call last):
  File "/u/*/.local/bin/pytest", line 8, in <module>
    sys.exit(main())
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 65, in main
    config = _prepareconfig(args, plugins)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 214, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 789, in pytest_cmdline_parse
    self.parse(args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 997, in parse
    self._preparse(args, addopts=addopts)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 953, in _preparse
    early_config=self, args=args, parser=self._parser
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 852, in pytest_load_initial_conftests
    self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 390, in _set_initial_conftests
    self._try_load_conftest(anchor)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 396, in _try_load_conftest
    self._getconftestmodules(anchor)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 431, in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 481, in _importconftest
    self.consider_conftest(mod)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 534, in consider_conftest
    self.register(conftestmodule, name=conftestmodule.__file__)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 328, in register
    ret = super(PytestPluginManager, self).register(plugin, name)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 120, in register
    self._verify_hook(hook, hookimpl)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 254, in _verify_hook
    notinspec,
pluggy.manager.PluginValidationError: Plugin '/tmp/test/conftest.py' for hook 'pytest_collection_modifyitems'
hookimpl definition: pytest_collection_modifyitems(config, items, request)
Argument(s) set(['request']) are declared in the hookimpl but can not be found in the hookspec
$ cat conftest.py 
import pytest

@pytest.fixture
def myfixture():
    t = 800
    yield t

def pytest_collection_modifyitems(config, items, item):
    f = item.funcargs['myfixture']
    pass


$ pytest test.py 
Traceback (most recent call last):
  File "/u/*/.local/bin/pytest", line 8, in <module>
    sys.exit(main())
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 65, in main
    config = _prepareconfig(args, plugins)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 214, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 789, in pytest_cmdline_parse
    self.parse(args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 997, in parse
    self._preparse(args, addopts=addopts)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 953, in _preparse
    early_config=self, args=args, parser=self._parser
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 852, in pytest_load_initial_conftests
    self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 390, in _set_initial_conftests
    self._try_load_conftest(anchor)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 396, in _try_load_conftest
    self._getconftestmodules(anchor)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 431, in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 481, in _importconftest
    self.consider_conftest(mod)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 534, in consider_conftest
    self.register(conftestmodule, name=conftestmodule.__file__)
  File "/u/*/.local/lib/python2.7/site-packages/_pytest/config/__init__.py", line 328, in register
    ret = super(PytestPluginManager, self).register(plugin, name)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 120, in register
    self._verify_hook(hook, hookimpl)
  File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 254, in _verify_hook
    notinspec,
pluggy.manager.PluginValidationError: Plugin '/tmp/test/conftest.py' for hook 'pytest_collection_modifyitems'
hookimpl definition: pytest_collection_modifyitems(config, items, item)
Argument(s) set(['item']) are declared in the hookimpl but can not be found in the hookspec
$ cat conftest.py 
import pytest

@pytest.fixture
def myfixture():
    t = 800
    yield t

def pytest_collection_modifyitems(config, items):
    for item in items:
        f = item.funcargs['myfixture']
    pass


$ pytest test.py 
=================================================================== test session starts ====================================================================
platform linux2 -- Python 2.7.18, pytest-4.6.11, py-1.11.0, pluggy-0.12.0
rootdir: /tmp/test
collected 1 item                                                                                                                                           
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/_pytest/main.py", line 206, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/_pytest/main.py", line 249, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/_pytest/main.py", line 259, in pytest_collection
INTERNALERROR>     return session.perform_collect()
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/_pytest/main.py", line 498, in perform_collect
INTERNALERROR>     session=self, config=self.config, items=items
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/hooks.py", line 289, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 87, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/manager.py", line 81, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/u/*/.local/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/tmp/test/conftest.py", line 10, in pytest_collection_modifyitems
INTERNALERROR>     f = item.funcargs['myfixture']
INTERNALERROR> KeyError: 'myfixture'

=============================================================== no tests ran in 0.01 seconds ===============================================================
0

There are 0 answers