@@ -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:
15
+ If you then have a text file like this:
20
16
21
- .. code-block :: ini
17
+ .. code-block :: text
22
18
23
- # content of pytest.ini
24
- [pytest]
25
- doctest_encoding = latin1
19
+ # content of test_example.txt
26
20
27
- The default encoding is UTF-8.
21
+ hello this is a doctest
22
+ >>> x = 3
23
+ >>> x
24
+ 3
28
25
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:
32
27
33
- .. code-block :: bash
34
-
35
- pytest --doctest-modules
28
+ .. code-block :: pytest
36
29
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
39
36
40
- .. code-block :: ini
37
+ mymodule.py . [100%]
41
38
42
- # content of pytest.ini
43
- [pytest]
44
- addopts = --doctest-modules
39
+ ========================= 1 passed in 0.12 seconds =========================
45
40
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).
47
43
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:
49
46
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
58
48
59
49
# content of mymodule.py
60
50
def something ():
@@ -64,36 +54,48 @@ and another like this::
64
54
"""
65
55
return 42
66
56
67
- then you can just invoke `` pytest `` without command line options:
57
+ .. code-block :: bash
68
58
69
- .. code-block :: pytest
59
+ $ pytest --doctest-modules
70
60
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:
77
63
78
- mymodule.py . [100%]
64
+ .. code-block :: ini
79
65
80
- ========================= 1 passed in 0.12 seconds =========================
66
+ # content of pytest.ini
67
+ [pytest]
68
+ addopts = --doctest-modules
81
69
82
- It is possible to use fixtures using the `` getfixture `` helper :
70
+ .. note : :
83
71
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.
85
76
86
- # content of example.rst
87
- >>> tmp = getfixture('tmpdir')
88
- >>> ...
89
- >>>
90
77
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
93
90
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
97
99
lengthy exception stack traces you can just write:
98
100
99
101
.. code-block :: ini
@@ -110,16 +112,7 @@ Python 3 unchanged:
110
112
* ``ALLOW_BYTES ``: when enabled, the ``b `` prefix is stripped from byte strings
111
113
in expected doctest output.
112
114
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
123
116
itself:
124
117
125
118
.. code-block :: rst
@@ -128,20 +121,58 @@ itself:
128
121
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
129
122
'Hello'
130
123
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
132
125
you want to continue the test even when you have failures, do:
133
126
134
127
.. code-block :: bash
135
128
136
129
pytest --doctest-modules --doctest-continue-on-failure
137
130
138
131
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
+ ------------------------
140
151
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.
143
155
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
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
145
176
146
177
The ``doctest_namespace `` fixture can be used to inject items into the
147
178
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
171
202
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
172
203
Fixtures will not be discovered in a sibling directory tree!
173
204
174
- Output format
175
- -------------
176
-
177
- .. versionadded :: 3.0
205
+ Skipping tests dynamically
206
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
178
207
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
183
209
184
- .. code-block :: bash
210
+ You can use `` pytest.skip `` to dynamically skip doctests. For example::
185
211
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