Skip to content

Feature request: Skip doctest conditionally #5273

@akaihola

Description

@akaihola

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions