scientific-python / pytest-doctestplus

Pytest plugin providing advanced doctest features
Other
94 stars 38 forks source link

Make skipping of doctests more visible? #246

Open lagru opened 6 months ago

lagru commented 6 months ago

We've started using doctestplus in scikit-image and I'm happy with it so far. However, my main gripe with it is that (conditional) skipping doctest isn't very visible. E.g. when I use __doctest_requires__ to mark functions then these are just silently not collected and ignored. Instead I would like to figure out if we can still collect these but mark them to be skipped the same way as if I added pytest.importorskip("pywt") to the doctest itself...

I could try looking into this but first I'd like to know if there are any concerns with this approach. And also were I should start..

lagru commented 6 months ago

Playing around with this, I think one approach would be to insert new lines into doctests during collection that cause pytest to skip. E.g.

--- pytest_doctestplus/plugin.py
+++ pytest_doctestplus/plugin.py
@@ -874,8 +874,14 @@
                         else:
                             continue  # The pattern does not apply

-                        if not self.check_required_modules(mods):
-                            return False
+                        for mod in mods:
+                            example = doctest.Example(
+                                source=f"import pytest; pytest.importorskip({mod!r})",
+                                want="",
+                                exc_msg=None,
+                            )
+                            test.examples.insert(0, example)
+
                 return True

             tests = list(filter(test_filter, tests))

Not sure how "hacky" you consider this approach but it seems to address my feature request perfectly.

pllim commented 6 months ago

I wonder if @nicoddemus & co could advise since we plug into doctest within pytest itself.

Any performance impact?

bsipocz commented 6 months ago

I agree that counting them into the skipped total would be nice (though big narrative docs file usually count as one, so partial skipping rst files may not be able to work this way)

lagru commented 6 months ago

Right, the suggested patch and our usage in scikit-image are mainly concerned with docstrings right now.