[pytest-dev] Hook validation difference between conftest files and plugins
holger at merlinux.eu
Mon Sep 29 08:47:29 CEST 2014
Hi Bruno, Anatoly,
On Sun, Sep 28, 2014 at 17:46 -0300, Bruno Oliveira wrote:
> Hi everyone,
> I'm having some trouble when trying to use some of xdist-defined hooks in a
> plugin, but I believe this problem is not related to xdist specifically.
> If I redefine a few of `xdist`'s hook functions inside a conftest file, it
> works fine. Once I move them to an external plugin however, I start getting
> "found unknown hook" errors.
> Here is an example:
> # contents of conftest.py
> def pytest_configure_node(node):
> assert 0, 'configure_node called'
> # contents of test_foo.py
> def test_foo(): pass
> In a fresh virtual environment with `pytest-xdist` installed:
> $ py.test -n2
> <snip traceback>
> INTERNALERROR> AssertionError: configure_node called
> OK, that's what I expected.
> On the other hand, renaming `conftest.py` to `myplugin.py`:
> $ py.test -n2 -p myplugin
> raise PluginValidationError("%s:\n%s" % (name, stringio.getvalue()))
> _pytest.helpconfig.PluginValidationError: myplugin:
> found unknown hook: pytest_configure_node
> (You might have to configure your $PYTHONPATH to the working directory)
> The same happens if I make a plugin using setup.py/setuptools.
> Am I missing something, or is this a bug?
> Digging at the source a bit I found that plugin registration and hook
> validation happen one right after the other, which might be a problem
> because a plugin might depend on hooks that will be registered by other
> plugins later.
You are right. We could do it differently by first calling pytest_addhooks
on all plugins before calling the pytest_registered hook. But Anatoly's
solution (deferring registration to pytest_configure) has the additional
advantage that you can conditionally register plugins. You might register
differently (or not at all) if there is no xdist installed, for example.
So currently i think it's best to just add an example to the docs.
> Pytest-dev mailing list
> Pytest-dev at python.org
More information about the Pytest-dev