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 request: Skip doctest conditionally #5273

Closed
akaihola opened this issue Aug 7, 2018 · 7 comments
Closed

Feature request: Skip doctest conditionally #5273

akaihola opened this issue Aug 7, 2018 · 7 comments
Labels
extensions type:proposal a feature suggestion
Milestone

Comments

@akaihola
Copy link
Contributor

akaihola commented Aug 7, 2018

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

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

The feature was also mentioned on Stack Overflow in the Sphinx doctest: conditional test skipping question.

Proposed syntax

.. doctest::
   :skipif: <condition>

   >>> test()
   <output>

.. testcode::
   :skipif: <condition>

   test()

.. testoutput::
   :skipif: <condition>

   <output>

Global setup

The code in the doctest_global_setup and doctest_global_cleanup configuration settings should be run before and after the condition. This would enable users to store complex conditions in a global variable and avoid repetition in doctest definitions.

Expected results

When a <condition> evaluates to a true value, the test or the test output assertion should be skipped.

@akaihola
Copy link
Contributor Author

akaihola commented Aug 7, 2018

In my patch above, I simply return an empty list from the run() method of the doctest directive classes in case the expression in the new :skipif: option evaluates to a true value.

I noticed that in the :pyversion: directive, a SKIP doctest flag is injected instead. However, this method only seems to be usable for the doctest:: directive but not for testcode:: and testoutput::. For this reason, I adopted the empty list method for all of the directives. As a side effect, conditional skipping can also be used for testsetup:: and testcleanup::.

If the proposed feature and my approach make sense, I'll proceed to writing tests and documentation next.

@tk0miya
Copy link
Member

tk0miya commented Aug 8, 2018

+0; I don't know doctest module so much. So I don't have any opinion. But it seems worthy.

ping @shimizukawa

@shimizukawa
Copy link
Member

+0. I am anxious about whether it will be too many options.
However, it seems good to have an option to exclude doctest under specific conditions from testing.

@tk0miya
Copy link
Member

tk0miya commented Aug 14, 2018

Okay, let's go forward.
@akaihola could you send us a PR please? It seems your patch is enough good. I will merge it soon if
you'll add testcode for it.

Thanks,

@tk0miya
Copy link
Member

tk0miya commented Aug 14, 2018

Note:
We'll call feature freeze for 1.8 in this weekend. It will be released as 1.8 if the patch will be merged until then.

@akaihola
Copy link
Contributor Author

@tk0miya, thanks for the note, I'll try to find time to work on this ASAP.

akaihola pushed a commit to akaihola/sphinx that referenced this issue Aug 16, 2018
akaihola pushed a commit to akaihola/sphinx that referenced this issue Aug 16, 2018
@akaihola
Copy link
Contributor Author

akaihola commented Aug 16, 2018

@tk0miya, I added both tests and documentation for the :skipif: option, and created pull request #5307.

akaihola pushed a commit to akaihola/sphinx that referenced this issue Aug 16, 2018
akaihola pushed a commit to akaihola/sphinx that referenced this issue Aug 16, 2018
akaihola pushed a commit to akaihola/sphinx that referenced this issue Aug 16, 2018
…p/cleanup

Ensure that `doctest_global_setup` and `doctest_global_cleanup` are executed
before and after evaluating each `:skipif:` option.
tk0miya added a commit that referenced this issue Aug 16, 2018
@tk0miya tk0miya added this to the 1.8.0 milestone Aug 16, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 12, 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

No branches or pull requests

3 participants