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

Bruno Oliveira nicoddemus at gmail.com
Wed Dec 7 13:23:46 PST 2016


Hi Christoph,

On Wed, Dec 7, 2016 at 11:51 AM Christoph Deil deil.christoph at googlemail.com
<http://mailto:deil.christoph@googlemail.com> wrote:

Are there any real pros and cons for the two options? Or are both equally
> powerful and it’s a matter of taste?
>
IMHO it seems to be a matter of taste. The requires_dependency function
still requires you to explicitly import the module inside the test file to
obtain the module object:

from gammapy.utils.testing import requires_dependency
@requires_dependency('scipy')def test_using_scipy():
    import scipy

Compared with pytest.importorskip:

import pytest
def test_using_scipy():
    scipy = pytest.importorskip('scipy')

So I think requires_dependency has the following cons:

   - It might be more explicit, since pytest.importorskip might be used
   deep inside the test function and easy to miss, while the decorator stands
   out;
   - It is more friendly to IDEs like PyCharm/PyDev, with code-completion,
   documentation, etc.

The only cons I can think of is that it requires one extra line and the
extra import (since it is common for tests to import pytest for other
reasons as well).

Do you think such a decorator would be appropriate for a pytest plugin?
>
If others find it useful, I don’t see why not. :)

Or even to be proposed as a convenience for pytest core?
>
I personally would be -0 on this because I feel it seems too similar to
using pytest.importorskip method so I would avoid having two ways of doing
the same thing. On the other hand, the implementation would be really
simple so it wouldn’t be a maintenance burden if others really like it.

Or would you just recommend we implement what we want in our own `utils.py`
> instead of trying to generalise a solution for this?
>
I think it is really commendable of you to try to provide a general
solution to the community, so please don’t get discouraged by my -0. And
since the implementation is simple and cookiecutter-pytest-plugin
<https://github.com/pytest-dev/cookiecutter-pytest-plugin> makes creating
plugins a snap, you should definitely go with it if you really prefer the
decoration method.

Cheers,
Bruno.
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161207/c7e26614/attachment.htm>


More information about the testing-in-python mailing list