[TIP] Add pytest requires_dependency decorator in addition to importorskip?

Bruno Oliveira nicoddemus at gmail.com
Thu Dec 8 03:55:45 PST 2016

Hi Christoph,

On Thu, Dec 8, 2016 at 6:19 AM Christoph Deil
[deil.christoph at googlemail.com](mailto:deil.christoph at googlemail.com)

Just to clarify: the way we handle optional dependencies in the (non-test)
code is to put them as delayed imports only inside the function or method
where they are used.

E.g. we have classes that have much functionality for computation, and then
one plot method and only that requires “import matplotlib”, because that
import is in the plot method.

So the extra “import scipy” line in the test code example you give above is
never there in our tests.

I see, thanks for the clarification.

One basic question I still have is what the advantage is of having this as
a Pytest plugin, instead of just a single-file .py module from which people
can import the requires_dependency decorator? Would this allow me to expose
the decorator in the top-level pytest or some other namespace? Is this a
good idea?

You can use the pytest_namespace
hook to expose decorator through pytest‘s own namespace so users can write

import pytest
@pytest.requires_dependency(‘scipy>=0.15, matplotlib’)def test_using_scipy():

That’s the same mechanism pytest itself uses to expose its own builtin
functionality like pytest.mark for example.

Are there one or a few examples of plugins that I could look at before
getting started that are similar to what I’m trying to do?

Can’t think of one out of the top of my head right now, sorry.

If you go ahead and implement a plugin, please make sure to announce here
in the list for those interested. :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161208/256ae8ca/attachment.html>

More information about the testing-in-python mailing list