Skip to content

Commit 67755d6

Browse files
committed
Review doctest docs
* Add pytest.skip() example * Add blurb about Sybil * Create a subsection for doctest-options * Create a subsection for pytest-specific features
1 parent 9c700d1 commit 67755d6

File tree

1 file changed

+108
-83
lines changed

1 file changed

+108
-83
lines changed

doc/en/doctest.rst

Lines changed: 108 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,41 @@ can change the pattern by issuing:
1010
1111
pytest --doctest-glob='*.rst'
1212
13-
on the command line. Since version ``2.9``, ``--doctest-glob``
14-
can be given multiple times in the command-line.
13+
on the command line. ``--doctest-glob`` can be given multiple times in the command-line.
1514

16-
.. versionadded:: 3.1
17-
18-
You can specify the encoding that will be used for those doctest files
19-
using the ``doctest_encoding`` ini option:
15+
If you then have a text file like this:
2016

21-
.. code-block:: ini
17+
.. code-block:: text
2218
23-
# content of pytest.ini
24-
[pytest]
25-
doctest_encoding = latin1
19+
# content of test_example.txt
2620
27-
The default encoding is UTF-8.
21+
hello this is a doctest
22+
>>> x = 3
23+
>>> x
24+
3
2825
29-
You can also trigger running of doctests
30-
from docstrings in all python modules (including regular
31-
python test modules):
26+
then you can just invoke ``pytest`` directly:
3227

33-
.. code-block:: bash
34-
35-
pytest --doctest-modules
28+
.. code-block:: pytest
3629
37-
You can make these changes permanent in your project by
38-
putting them into a pytest.ini file like this:
30+
$ pytest
31+
=========================== test session starts ============================
32+
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
33+
cachedir: $PYTHON_PREFIX/.pytest_cache
34+
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
35+
collected 1 item
3936
40-
.. code-block:: ini
37+
mymodule.py . [100%]
4138
42-
# content of pytest.ini
43-
[pytest]
44-
addopts = --doctest-modules
39+
========================= 1 passed in 0.12 seconds =========================
4540
46-
If you then have a text file like this:
41+
By default, pytest will collect ``test*.txt`` files looking for doctest directives, but you
42+
can pass additional globs using the ``--doctest-glob`` option (multi-allowed).
4743

48-
.. code-block:: text
44+
In addition to text files, you can also execute doctests directly from docstrings of your classes
45+
and functions, including from test modules:
4946

50-
# content of example.rst
51-
52-
hello this is a doctest
53-
>>> x = 3
54-
>>> x
55-
3
56-
57-
and another like this::
47+
.. code-block:: python
5848
5949
# content of mymodule.py
6050
def something():
@@ -64,36 +54,48 @@ and another like this::
6454
"""
6555
return 42
6656
67-
then you can just invoke ``pytest`` without command line options:
57+
.. code-block:: bash
6858
69-
.. code-block:: pytest
59+
$ pytest --doctest-modules
7060
71-
$ pytest
72-
=========================== test session starts ============================
73-
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
74-
cachedir: $PYTHON_PREFIX/.pytest_cache
75-
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
76-
collected 1 item
61+
You can make these changes permanent in your project by
62+
putting them into a pytest.ini file like this:
7763

78-
mymodule.py . [100%]
64+
.. code-block:: ini
7965
80-
========================= 1 passed in 0.12 seconds =========================
66+
# content of pytest.ini
67+
[pytest]
68+
addopts = --doctest-modules
8169
82-
It is possible to use fixtures using the ``getfixture`` helper:
70+
.. note::
8371

84-
.. code-block:: text
72+
If you are making serious use of doctests through your code, please checkout
73+
`Sybil <https://sybil.readthedocs.io/en/latest/index.html>`__, which besides
74+
supporting doctests also supports Sphinx directives, plus other features.
75+
It provides pytest integration out of the box.
8576

86-
# content of example.rst
87-
>>> tmp = getfixture('tmpdir')
88-
>>> ...
89-
>>>
9077

91-
Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported
92-
when executing text doctest files.
78+
Encoding
79+
--------
80+
81+
The default encoding is **UTF-8**, but you can specify the encoding
82+
that will be used for those doctest files using the
83+
``doctest_encoding`` ini option:
84+
85+
.. code-block:: ini
86+
87+
# content of pytest.ini
88+
[pytest]
89+
doctest_encoding = latin1
9390
94-
The standard ``doctest`` module provides some setting flags to configure the
95-
strictness of doctest tests. In pytest, you can enable those flags using the
96-
configuration file. To make pytest ignore trailing whitespaces and ignore
91+
Using 'doctest' options
92+
-----------------------
93+
94+
The standard ``doctest`` module provides some `options <https://docs.python.org/3/library/doctest.html#option-flags>`__
95+
to configure the strictness of doctest tests. In pytest, you can enable those flags using the
96+
configuration file.
97+
98+
For example, to make pytest ignore trailing whitespaces and ignore
9799
lengthy exception stack traces you can just write:
98100

99101
.. code-block:: ini
@@ -110,16 +112,7 @@ Python 3 unchanged:
110112
* ``ALLOW_BYTES``: when enabled, the ``b`` prefix is stripped from byte strings
111113
in expected doctest output.
112114

113-
As with any other option flag, these flags can be enabled in ``pytest.ini`` using
114-
the ``doctest_optionflags`` ini option:
115-
116-
.. code-block:: ini
117-
118-
[pytest]
119-
doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES
120-
121-
122-
Alternatively, it can be enabled by an inline comment in the doc test
115+
Alternatively, options can be enabled by an inline comment in the doc test
123116
itself:
124117

125118
.. code-block:: rst
@@ -128,20 +121,58 @@ itself:
128121
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
129122
'Hello'
130123
131-
By default, pytest would report only the first failure for a given doctest. If
124+
By default, pytest would report only the first failure for a given doctest. If
132125
you want to continue the test even when you have failures, do:
133126

134127
.. code-block:: bash
135128
136129
pytest --doctest-modules --doctest-continue-on-failure
137130
138131
139-
.. _`doctest_namespace`:
132+
Output format
133+
-------------
134+
135+
You can change the diff output format on failure for your doctests
136+
by using one of standard doctest modules format in options
137+
(see :data:`python:doctest.REPORT_UDIFF`, :data:`python:doctest.REPORT_CDIFF`,
138+
:data:`python:doctest.REPORT_NDIFF`, :data:`python:doctest.REPORT_ONLY_FIRST_FAILURE`):
139+
140+
.. code-block:: bash
141+
142+
pytest --doctest-modules --doctest-report none
143+
pytest --doctest-modules --doctest-report udiff
144+
pytest --doctest-modules --doctest-report cdiff
145+
pytest --doctest-modules --doctest-report ndiff
146+
pytest --doctest-modules --doctest-report only_first_failure
147+
148+
149+
pytest-specific features
150+
------------------------
140151

141-
The 'doctest_namespace' fixture
142-
-------------------------------
152+
Some features are provided to make writing doctests easier or with better integration with
153+
your existing test suite. Keep in mind however that by using those features you will make
154+
your doctests incompatible with the standard ``doctests`` module.
143155

144-
.. versionadded:: 3.0
156+
Using fixtures
157+
^^^^^^^^^^^^^^
158+
159+
It is possible to use fixtures using the ``getfixture`` helper:
160+
161+
.. code-block:: text
162+
163+
# content of example.rst
164+
>>> tmp = getfixture('tmpdir')
165+
>>> ...
166+
>>>
167+
168+
Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported
169+
when executing text doctest files.
170+
171+
172+
.. _`doctest_namespace`:
173+
174+
'doctest_namespace' fixture
175+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
145176

146177
The ``doctest_namespace`` fixture can be used to inject items into the
147178
namespace in which your doctests run. It is intended to be used within
@@ -171,20 +202,14 @@ Note that like the normal ``conftest.py``, the fixtures are discovered in the di
171202
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
172203
Fixtures will not be discovered in a sibling directory tree!
173204

174-
Output format
175-
-------------
176-
177-
.. versionadded:: 3.0
205+
Skipping tests dynamically
206+
^^^^^^^^^^^^^^^^^^^^^^^^^^
178207

179-
You can change the diff output format on failure for your doctests
180-
by using one of standard doctest modules format in options
181-
(see :data:`python:doctest.REPORT_UDIFF`, :data:`python:doctest.REPORT_CDIFF`,
182-
:data:`python:doctest.REPORT_NDIFF`, :data:`python:doctest.REPORT_ONLY_FIRST_FAILURE`):
208+
.. versionadded:: 4.4
183209

184-
.. code-block:: bash
210+
You can use ``pytest.skip`` to dynamically skip doctests. For example::
185211

186-
pytest --doctest-modules --doctest-report none
187-
pytest --doctest-modules --doctest-report udiff
188-
pytest --doctest-modules --doctest-report cdiff
189-
pytest --doctest-modules --doctest-report ndiff
190-
pytest --doctest-modules --doctest-report only_first_failure
212+
>>> import sys, pytest
213+
>>> if sys.platform.startswith('win'):
214+
... pytest.skip('this doctest does not work on Windows')
215+
...

0 commit comments

Comments
 (0)