[pytest-dev] Hook validation difference between conftest files and plugins

Bruno Oliveira nicoddemus at gmail.com
Mon Sep 29 12:56:01 CEST 2014


Hi Anatoly, Holger,

On Sun, Sep 28, 2014 at 6:38 PM, Anatoly Bubenkov <bubenkoff at gmail.com>
 wrote:
> I had the same problem solved it by 'deferring' a plugin hook a bit:

Thanks! I will use this approach.


On Mon, Sep 29, 2014 at 3:47 AM, holger krekel <holger at merlinux.eu> wrote:
> So currently i think it's best to just add an example to the docs.

OK, I will open a PR with an example targeted to plugin developers.

Thanks for the help!

Cheers,


On Mon, Sep 29, 2014 at 3:47 AM, holger krekel <holger at merlinux.eu> wrote:

> 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.
>
> best,
> holge
>
>
>
> > Cheers,
> > Bruno.
>
> > _______________________________________________
> > Pytest-dev mailing list
> > Pytest-dev at python.org
> > https://mail.python.org/mailman/listinfo/pytest-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20140929/10336d8b/attachment.html>


More information about the Pytest-dev mailing list