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

Christoph Deil deil.christoph at googlemail.com
Wed Dec 7 05:48:27 PST 2016


Hi,

Astropy and other packages have a lot of tests (using pytest) that require optional dependencies.

So far the approach for marking such tests has been this:
http://astropy.readthedocs.io/en/latest/development/testguide.html#tests-requiring-optional-dependencies <http://astropy.readthedocs.io/en/latest/development/testguide.html#tests-requiring-optional-dependencies>
which has lead to a lot of boilerplate try-except import code at the top of test files.

We’re now discussing how to simplify this and would like your input before taking a decision.
See https://github.com/astropy/astropy/issues/5543 <https://github.com/astropy/astropy/issues/5543>

To summarise, one proposal is to use pytest.importorskip and put those statement as first line in the test functions.
http://doc.pytest.org/en/latest/skipping.html#skipping-on-a-missing-import-dependency <http://doc.pytest.org/en/latest/skipping.html#skipping-on-a-missing-import-dependency>

An alternative proposal is to define a decorator that wraps “pytest.mark.skipif”,
see https://github.com/astropy/astropy/issues/5543#issuecomment-265434710 <https://github.com/astropy/astropy/issues/5543#issuecomment-265434710> and the following comments.

It was pointed out that at least one other project came up independently with a similar solution of wrapping “pytest.mark.skipif”:
https://github.com/glue-viz/glue/blob/master/glue/tests/helpers.py#L14 <https://github.com/glue-viz/glue/blob/master/glue/tests/helpers.py#L14>

In my opinion a declarative syntax with a decorator line before the function is more obvious / nicer than putting a pytest.importorskip inside the test function.

So here’s my concrete questions:

Are there any real pros and cons for the two options? Or are both equally powerful and it’s a matter of taste?

Do you think such a decorator would be appropriate for a pytest plugin?
Or even to be proposed as a convenience for pytest core?
Or would you just recommend we implement what we want in our own `utils.py` instead of trying to generalise a solution for this?

Thank you in advance!

Christoph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161207/455ab459/attachment.html>


More information about the testing-in-python mailing list