@@ -253,16 +253,53 @@ Note that like the normal ``conftest.py``, the fixtures are discovered in the di
253
253
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
254
254
Fixtures will not be discovered in a sibling directory tree!
255
255
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
+ """
258
275
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:
260
281
261
- You can use ``pytest.skip `` to dynamically skip doctests. For example:
262
282
263
283
.. code-block :: text
264
284
265
285
>>> import sys, pytest
266
286
>>> if sys.platform.startswith('win'):
267
287
... pytest.skip('this doctest does not work on Windows')
268
288
...
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