Skip to content

Commit 3405c7e

Browse files
Add more info about skipping doctests (#8080)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 775ba63 commit 3405c7e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Philipp Loose
239239
Pieter Mulder
240240
Piotr Banaszkiewicz
241241
Piotr Helm
242+
Prakhar Gurunani
242243
Prashant Anand
243244
Prashant Sharma
244245
Pulkit Goyal

changelog/7429.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add more information and use cases about skipping doctests.

doc/en/doctest.rst

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,53 @@ Note that like the normal ``conftest.py``, the fixtures are discovered in the di
253253
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
254254
Fixtures will not be discovered in a sibling directory tree!
255255

256-
Skipping tests dynamically
257-
^^^^^^^^^^^^^^^^^^^^^^^^^^
256+
Skipping tests
257+
^^^^^^^^^^^^^^
258+
259+
For the same reasons one might want to skip normal tests, it is also possible to skip
260+
tests inside doctests.
261+
262+
To skip a single check inside a doctest you can use the standard
263+
`doctest.SKIP <https://docs.python.org/3/library/doctest.html#doctest.SKIP>`__ directive:
264+
265+
.. code-block:: python
266+
267+
def test_random(y):
268+
"""
269+
>>> random.random() # doctest: +SKIP
270+
0.156231223
271+
272+
>>> 1 + 1
273+
2
274+
"""
258275
259-
.. versionadded:: 4.4
276+
This will skip the first check, but not the second.
277+
278+
pytest also allows using the standard pytest functions :func:`pytest.skip` and
279+
:func:`pytest.xfail` inside doctests, which might be useful because you can
280+
then skip/xfail tests based on external conditions:
260281

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

263283
.. code-block:: text
264284
265285
>>> import sys, pytest
266286
>>> if sys.platform.startswith('win'):
267287
... pytest.skip('this doctest does not work on Windows')
268288
...
289+
>>> import fcntl
290+
>>> ...
291+
292+
However using those functions is discouraged because it reduces the readability of the
293+
docstring.
294+
295+
.. note::
296+
297+
:func:`pytest.skip` and :func:`pytest.xfail` behave differently depending
298+
if the doctests are in a Python file (in docstrings) or a text file containing
299+
doctests intermingled with text:
300+
301+
* Python modules (docstrings): the functions only act in that specific docstring,
302+
letting the other docstrings in the same module execute as normal.
303+
304+
* Text files: the functions will skip/xfail the checks for the rest of the entire
305+
file.

0 commit comments

Comments
 (0)