@@ -10,51 +10,41 @@ can change the pattern by issuing:
10
10
11
11
pytest --doctest-glob=' *.rst'
12
12
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.
15
14
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:
20
-
21
- .. code-block :: ini
22
-
23
- # content of pytest.ini
24
- [pytest]
25
- doctest_encoding = latin1
26
-
27
- The default encoding is UTF-8.
15
+ If you then have a text file like this:
28
16
29
- You can also trigger running of doctests
30
- from docstrings in all python modules (including regular
31
- python test modules):
17
+ .. code-block :: text
32
18
33
- .. code-block :: bash
19
+ # content of test_example.txt
34
20
35
- pytest --doctest-modules
21
+ hello this is a doctest
22
+ >>> x = 3
23
+ >>> x
24
+ 3
36
25
37
- You can make these changes permanent in your project by
38
- putting them into a pytest.ini file like this:
26
+ then you can just invoke ``pytest `` directly:
39
27
40
- .. code-block :: ini
28
+ .. code-block :: pytest
41
29
42
- # content of pytest.ini
43
- [pytest]
44
- addopts = --doctest-modules
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
35
+ collected 1 item
45
36
46
- If you then have a text file like this:
37
+ test_example.txt . [100%]
47
38
48
- .. code-block :: text
39
+ ========================= 1 passed in 0.12 seconds =========================
49
40
50
- # content of example.rst
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).
51
43
52
- hello this is a doctest
53
- >>> x = 3
54
- >>> x
55
- 3
44
+ In addition to text files, you can also execute doctests directly from docstrings of your classes
45
+ and functions, including from test modules:
56
46
57
- and another like this::
47
+ .. code-block :: python
58
48
59
49
# content of mymodule.py
60
50
def something ():
@@ -64,36 +54,60 @@ and another like this::
64
54
"""
65
55
return 42
66
56
67
- then you can just invoke ``pytest `` without command line options:
68
-
69
- .. code-block :: pytest
57
+ .. code-block :: bash
70
58
71
- $ pytest
59
+ $ pytest --doctest-modules
72
60
=========================== test session starts ============================
73
61
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
74
62
cachedir: $PYTHON_PREFIX /.pytest_cache
75
- rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
76
- collected 1 item
63
+ rootdir: $REGENDOC_TMPDIR
64
+ collected 2 items
77
65
78
- mymodule.py . [100%]
66
+ mymodule.py . [ 50%]
67
+ test_example.txt . [100%]
79
68
80
- ========================= 1 passed in 0.12 seconds =========================
69
+ ========================= 2 passed in 0.12 seconds =========================
81
70
82
- It is possible to use fixtures using the ``getfixture `` helper:
71
+ You can make these changes permanent in your project by
72
+ putting them into a pytest.ini file like this:
83
73
84
- .. code-block :: text
74
+ .. code-block :: ini
85
75
86
- # content of example.rst
87
- >>> tmp = getfixture('tmpdir')
88
- >>> ...
89
- >>>
76
+ # content of pytest.ini
77
+ [pytest]
78
+ addopts = --doctest-modules
90
79
91
- Also, :ref: `usefixtures ` and :ref: `autouse ` fixtures are supported
92
- when executing text doctest files.
80
+ .. note ::
93
81
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
82
+ The builtin pytest doctest supports only ``doctest `` blocks, but if you are looking
83
+ for more advanced checking over *all * your documentation,
84
+ including doctests, ``.. codeblock:: python `` Sphinx directive support,
85
+ and any other examples your documentation may include, you may wish to
86
+ consider `Sybil <https://sybil.readthedocs.io/en/latest/index.html >`__.
87
+ It provides pytest integration out of the box.
88
+
89
+
90
+ Encoding
91
+ --------
92
+
93
+ The default encoding is **UTF-8 **, but you can specify the encoding
94
+ that will be used for those doctest files using the
95
+ ``doctest_encoding `` ini option:
96
+
97
+ .. code-block :: ini
98
+
99
+ # content of pytest.ini
100
+ [pytest]
101
+ doctest_encoding = latin1
102
+
103
+ Using 'doctest' options
104
+ -----------------------
105
+
106
+ The standard ``doctest `` module provides some `options <https://docs.python.org/3/library/doctest.html#option-flags >`__
107
+ to configure the strictness of doctest tests. In pytest, you can enable those flags using the
108
+ configuration file.
109
+
110
+ For example, to make pytest ignore trailing whitespaces and ignore
97
111
lengthy exception stack traces you can just write:
98
112
99
113
.. code-block :: ini
@@ -110,16 +124,7 @@ Python 3 unchanged:
110
124
* ``ALLOW_BYTES ``: when enabled, the ``b `` prefix is stripped from byte strings
111
125
in expected doctest output.
112
126
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
127
+ Alternatively, options can be enabled by an inline comment in the doc test
123
128
itself:
124
129
125
130
.. code-block :: rst
@@ -128,20 +133,58 @@ itself:
128
133
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
129
134
'Hello'
130
135
131
- By default, pytest would report only the first failure for a given doctest. If
136
+ By default, pytest would report only the first failure for a given doctest. If
132
137
you want to continue the test even when you have failures, do:
133
138
134
139
.. code-block :: bash
135
140
136
141
pytest --doctest-modules --doctest-continue-on-failure
137
142
138
143
139
- .. _`doctest_namespace` :
144
+ Output format
145
+ -------------
146
+
147
+ You can change the diff output format on failure for your doctests
148
+ by using one of standard doctest modules format in options
149
+ (see :data: `python:doctest.REPORT_UDIFF `, :data: `python:doctest.REPORT_CDIFF `,
150
+ :data: `python:doctest.REPORT_NDIFF `, :data: `python:doctest.REPORT_ONLY_FIRST_FAILURE `):
151
+
152
+ .. code-block :: bash
153
+
154
+ pytest --doctest-modules --doctest-report none
155
+ pytest --doctest-modules --doctest-report udiff
156
+ pytest --doctest-modules --doctest-report cdiff
157
+ pytest --doctest-modules --doctest-report ndiff
158
+ pytest --doctest-modules --doctest-report only_first_failure
140
159
141
- The 'doctest_namespace' fixture
142
- -------------------------------
143
160
144
- .. versionadded :: 3.0
161
+ pytest-specific features
162
+ ------------------------
163
+
164
+ Some features are provided to make writing doctests easier or with better integration with
165
+ your existing test suite. Keep in mind however that by using those features you will make
166
+ your doctests incompatible with the standard ``doctests `` module.
167
+
168
+ Using fixtures
169
+ ^^^^^^^^^^^^^^
170
+
171
+ It is possible to use fixtures using the ``getfixture `` helper:
172
+
173
+ .. code-block :: text
174
+
175
+ # content of example.rst
176
+ >>> tmp = getfixture('tmpdir')
177
+ >>> ...
178
+ >>>
179
+
180
+ Also, :ref: `usefixtures ` and :ref: `autouse ` fixtures are supported
181
+ when executing text doctest files.
182
+
183
+
184
+ .. _`doctest_namespace` :
185
+
186
+ 'doctest_namespace' fixture
187
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
145
188
146
189
The ``doctest_namespace `` fixture can be used to inject items into the
147
190
namespace in which your doctests run. It is intended to be used within
@@ -171,20 +214,14 @@ Note that like the normal ``conftest.py``, the fixtures are discovered in the di
171
214
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
172
215
Fixtures will not be discovered in a sibling directory tree!
173
216
174
- Output format
175
- -------------
176
-
177
- .. versionadded :: 3.0
217
+ Skipping tests dynamically
218
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
178
219
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 `):
220
+ .. versionadded :: 4.4
183
221
184
- .. code-block :: bash
222
+ You can use `` pytest.skip `` to dynamically skip doctests. For example::
185
223
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
224
+ >>> import sys, pytest
225
+ >>> if sys.platform.startswith('win'):
226
+ ... pytest.skip('this doctest does not work on Windows')
227
+ ...
0 commit comments