Skip to content

Commit

Permalink
Add more info about skipping doctests (pytest-dev#8080)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
FirePing32 and nicoddemus authored Nov 28, 2020
1 parent 775ba63 commit 3405c7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ Philipp Loose
Pieter Mulder
Piotr Banaszkiewicz
Piotr Helm
Prakhar Gurunani
Prashant Anand
Prashant Sharma
Pulkit Goyal
Expand Down
1 change: 1 addition & 0 deletions changelog/7429.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add more information and use cases about skipping doctests.
45 changes: 41 additions & 4 deletions doc/en/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,53 @@ Note that like the normal ``conftest.py``, the fixtures are discovered in the di
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
Fixtures will not be discovered in a sibling directory tree!

Skipping tests dynamically
^^^^^^^^^^^^^^^^^^^^^^^^^^
Skipping tests
^^^^^^^^^^^^^^

For the same reasons one might want to skip normal tests, it is also possible to skip
tests inside doctests.

To skip a single check inside a doctest you can use the standard
`doctest.SKIP <https://docs.python.org/3/library/doctest.html#doctest.SKIP>`__ directive:

.. code-block:: python
def test_random(y):
"""
>>> random.random() # doctest: +SKIP
0.156231223
>>> 1 + 1
2
"""
.. versionadded:: 4.4
This will skip the first check, but not the second.

pytest also allows using the standard pytest functions :func:`pytest.skip` and
:func:`pytest.xfail` inside doctests, which might be useful because you can
then skip/xfail tests based on external conditions:

You can use ``pytest.skip`` to dynamically skip doctests. For example:

.. code-block:: text
>>> import sys, pytest
>>> if sys.platform.startswith('win'):
... pytest.skip('this doctest does not work on Windows')
...
>>> import fcntl
>>> ...
However using those functions is discouraged because it reduces the readability of the
docstring.

.. note::

:func:`pytest.skip` and :func:`pytest.xfail` behave differently depending
if the doctests are in a Python file (in docstrings) or a text file containing
doctests intermingled with text:

* Python modules (docstrings): the functions only act in that specific docstring,
letting the other docstrings in the same module execute as normal.

* Text files: the functions will skip/xfail the checks for the rest of the entire
file.

0 comments on commit 3405c7e

Please sign in to comment.