Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: skip doctests conditionally #5307

Merged
merged 6 commits into from
Aug 16, 2018

Conversation

akaihola
Copy link
Contributor

Subject: A :skipif: option for doctest directives

Feature or Bugfix

  • Feature

Purpose

  • Unit tests can be skipped conditionally using the @unittest.skipIf() decorator. A similar feature would be useful for doctests when run by Sphinx.

Detail

Example use cases:

  • different tests for specific versions of dependencies
  • skip tests in absence of optional dependencies
  • skip tests in absence of a network or VPN
  • skip tests in absence of some hardware

Relates

@codecov
Copy link

codecov bot commented Aug 16, 2018

Codecov Report

Merging #5307 into master will increase coverage by 0.02%.
The diff coverage is 82.6%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5307      +/-   ##
==========================================
+ Coverage   82.21%   82.24%   +0.02%     
==========================================
  Files         296      296              
  Lines       39346    39367      +21     
  Branches     6058     6063       +5     
==========================================
+ Hits        32348    32376      +28     
+ Misses       5669     5663       -6     
+ Partials     1329     1328       -1
Impacted Files Coverage Δ
tests/test_ext_doctest.py 92.72% <81.81%> (-2.73%) ⬇️
sphinx/ext/doctest.py 83.87% <83.33%> (+3.2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bce956d...098f37f. Read the comment docs.

…p/cleanup

Ensure that `doctest_global_setup` and `doctest_global_cleanup` are executed
before and after evaluating each `:skipif:` option.
@tk0miya tk0miya added extensions type:proposal a feature suggestion labels Aug 16, 2018
@tk0miya tk0miya added this to the 1.8.0 milestone Aug 16, 2018
Copy link
Member

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! LGTM!

@tk0miya tk0miya merged commit 28131df into sphinx-doc:master Aug 16, 2018
@tk0miya
Copy link
Member

tk0miya commented Aug 16, 2018

Merged.
Thank you for great contribution!

@akaihola
Copy link
Contributor Author

Thank you @tk0miya, I'm really happy to get this feature in. And the experience of contributing to Sphinx was one of the best I've had with any Open Source project. Great work guys!

akaihola added a commit to akaihola/sphinx that referenced this pull request Sep 13, 2018
I forgot to update `AUTHORS` in sphinx-doc#5307.
@akaihola akaihola mentioned this pull request Sep 13, 2018
@timokau
Copy link

timokau commented Jan 20, 2019

Is it somehow possible to add the :skipif: option to doctest blocks (i.e. without explicit .. doctest:: directive)?

Example:

>>> print(42)
42

How can I skip this test conditionally? The only option I found is adding .. doctest::, but that makes it necessary to change indentation as it apparently has to have one indentation level less than the doctest block.

@akaihola akaihola deleted the 5273_doctest_conditional_skip branch January 20, 2019 19:29
@akaihola
Copy link
Contributor Author

@timokau, hi, I'm not 100% sure, but I would guess that it's not possible to apply :skipif: to such doctest blocks.

Note that the code in a .. doctest:: block can be indented by three or more spaces. So a four-space indent works just as well!

@timokau
Copy link

timokau commented Jan 20, 2019

Okay, thanks for the reply. I'll just live with the indentation "issue" then. For what its worth, this illustrates the issue I'm talking about:

The indentation issue is:

def fn():
"""
This does something.

Examples:

	Here it prints something:

.. doctest::
   :skipif: True

	>>> fn()
	"something"

"""

@wojdyr
Copy link
Contributor

wojdyr commented Feb 8, 2019

Thanks for this feature!

I just realized that :skipif: is evaluated also when building documentation (make html) not just during make doctest. So if a certain module is missing on ReadTheDocs then corresponding doctest blocks are not included in documentation. This is easy to workaround, but I wonder if it's intentional?

@akaihola
Copy link
Contributor Author

@wojdyr, that's a really good point. Skipping on make html was not intentional. I remember it kind of occurring to me at some point, but apparently I forgot about it. Feel free to create another issue about this, it would very much make sense to fix it.

@wojdyr
Copy link
Contributor

wojdyr commented Feb 13, 2019

@akaihola I've opened #6068 .
Thanks again for :skipif:, it solved the problem I had with doctest blocks.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
extensions type:proposal a feature suggestion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants