2021-03-12 04:30:47 +08:00
|
|
|
.. _doctest:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2021-03-15 16:22:11 +08:00
|
|
|
How to run doctests
|
2010-10-11 05:45:45 +08:00
|
|
|
=========================================================
|
|
|
|
|
2020-10-02 21:39:15 +08:00
|
|
|
By default, all files matching the ``test*.txt`` pattern will
|
2021-08-26 22:05:03 +08:00
|
|
|
be run through the python standard :mod:`doctest` module. You
|
2019-02-13 23:08:49 +08:00
|
|
|
can change the pattern by issuing:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-02-13 23:08:49 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-04-29 04:04:58 +08:00
|
|
|
pytest --doctest-glob="*.rst"
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
on the command line. ``--doctest-glob`` can be given multiple times in the command-line.
|
2015-12-31 04:19:08 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
If you then have a text file like this:
|
2016-11-30 21:17:54 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
.. code-block:: text
|
2016-12-01 01:01:00 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
# content of test_example.txt
|
2016-11-29 18:51:56 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
hello this is a doctest
|
|
|
|
>>> x = 3
|
|
|
|
>>> x
|
|
|
|
3
|
2016-11-29 18:51:56 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
then you can just invoke ``pytest`` directly:
|
2019-02-13 23:08:49 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
.. code-block:: pytest
|
2016-08-23 10:35:41 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
$ pytest
|
|
|
|
=========================== test session starts ============================
|
2021-08-31 01:40:59 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.y
|
2019-04-28 22:54:29 +08:00
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
2019-04-28 23:11:53 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR
|
2019-04-28 22:54:29 +08:00
|
|
|
collected 1 item
|
2015-07-10 08:50:38 +08:00
|
|
|
|
2019-04-28 23:11:53 +08:00
|
|
|
test_example.txt . [100%]
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-08-30 23:43:47 +08:00
|
|
|
============================ 1 passed in 0.12s =============================
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
By default, pytest will collect ``test*.txt`` files looking for doctest directives, but you
|
|
|
|
can pass additional globs using the ``--doctest-glob`` option (multi-allowed).
|
2019-02-13 23:08:49 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
In addition to text files, you can also execute doctests directly from docstrings of your classes
|
|
|
|
and functions, including from test modules:
|
2010-10-11 16:07:14 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
.. code-block:: python
|
2010-10-11 16:07:14 +08:00
|
|
|
|
|
|
|
# content of mymodule.py
|
|
|
|
def something():
|
2020-12-30 17:56:09 +08:00
|
|
|
"""a doctest in a docstring
|
2010-11-26 20:26:56 +08:00
|
|
|
>>> something()
|
2010-10-11 16:07:14 +08:00
|
|
|
42
|
|
|
|
"""
|
|
|
|
return 42
|
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
.. code-block:: bash
|
2018-11-24 13:41:22 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
$ pytest --doctest-modules
|
2019-04-28 23:11:53 +08:00
|
|
|
=========================== test session starts ============================
|
2021-08-31 01:40:59 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.y
|
2019-04-28 23:11:53 +08:00
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
|
|
|
rootdir: $REGENDOC_TMPDIR
|
|
|
|
collected 2 items
|
|
|
|
|
|
|
|
mymodule.py . [ 50%]
|
|
|
|
test_example.txt . [100%]
|
|
|
|
|
2019-08-30 23:43:47 +08:00
|
|
|
============================ 2 passed in 0.12s =============================
|
2010-10-11 16:07:14 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
You can make these changes permanent in your project by
|
|
|
|
putting them into a pytest.ini file like this:
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
.. code-block:: ini
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
# content of pytest.ini
|
|
|
|
[pytest]
|
|
|
|
addopts = --doctest-modules
|
2013-03-21 00:54:38 +08:00
|
|
|
|
2013-11-22 22:35:20 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
Encoding
|
|
|
|
--------
|
|
|
|
|
|
|
|
The default encoding is **UTF-8**, but you can specify the encoding
|
|
|
|
that will be used for those doctest files using the
|
|
|
|
``doctest_encoding`` ini option:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
# content of pytest.ini
|
|
|
|
[pytest]
|
|
|
|
doctest_encoding = latin1
|
2014-10-08 21:48:41 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
Using 'doctest' options
|
|
|
|
-----------------------
|
|
|
|
|
2021-08-26 22:05:03 +08:00
|
|
|
Python's standard :mod:`doctest` module provides some :ref:`options <python:option-flags-and-directives>`
|
2019-04-28 22:54:29 +08:00
|
|
|
to configure the strictness of doctest tests. In pytest, you can enable those flags using the
|
|
|
|
configuration file.
|
|
|
|
|
|
|
|
For example, to make pytest ignore trailing whitespaces and ignore
|
2018-01-04 21:47:05 +08:00
|
|
|
lengthy exception stack traces you can just write:
|
2015-12-30 07:05:11 +08:00
|
|
|
|
|
|
|
.. code-block:: ini
|
2014-10-08 21:48:41 +08:00
|
|
|
|
|
|
|
[pytest]
|
2020-10-27 03:30:00 +08:00
|
|
|
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2019-07-09 00:00:32 +08:00
|
|
|
Alternatively, options can be enabled by an inline comment in the doc test
|
|
|
|
itself:
|
|
|
|
|
|
|
|
.. code-block:: rst
|
|
|
|
|
|
|
|
>>> something_that_raises() # doctest: +IGNORE_EXCEPTION_DETAIL
|
|
|
|
Traceback (most recent call last):
|
|
|
|
ValueError: ...
|
|
|
|
|
2019-07-08 22:41:33 +08:00
|
|
|
pytest also introduces new options:
|
2015-12-30 07:05:11 +08:00
|
|
|
|
|
|
|
* ``ALLOW_UNICODE``: when enabled, the ``u`` prefix is stripped from unicode
|
2019-07-08 22:41:33 +08:00
|
|
|
strings in expected doctest output. This allows doctests to run in Python 2
|
|
|
|
and Python 3 unchanged.
|
2015-12-30 07:05:11 +08:00
|
|
|
|
2019-07-08 22:41:33 +08:00
|
|
|
* ``ALLOW_BYTES``: similarly, the ``b`` prefix is stripped from byte strings
|
2015-12-30 07:05:11 +08:00
|
|
|
in expected doctest output.
|
|
|
|
|
2019-07-08 22:41:33 +08:00
|
|
|
* ``NUMBER``: when enabled, floating-point numbers only need to match as far as
|
|
|
|
the precision you have written in the expected doctest output. For example,
|
|
|
|
the following output would only need to match to 2 decimal places::
|
|
|
|
|
|
|
|
>>> math.pi
|
|
|
|
3.14
|
|
|
|
|
|
|
|
If you wrote ``3.1416`` then the actual output would need to match to 4
|
|
|
|
decimal places; and so on.
|
|
|
|
|
|
|
|
This avoids false positives caused by limited floating-point precision, like
|
|
|
|
this::
|
|
|
|
|
|
|
|
Expected:
|
|
|
|
0.233
|
|
|
|
Got:
|
|
|
|
0.23300000000000001
|
|
|
|
|
|
|
|
``NUMBER`` also supports lists of floating-point numbers -- in fact, it
|
2019-07-11 17:04:43 +08:00
|
|
|
matches floating-point numbers appearing anywhere in the output, even inside
|
|
|
|
a string! This means that it may not be appropriate to enable globally in
|
|
|
|
``doctest_optionflags`` in your configuration file.
|
2019-02-13 23:08:49 +08:00
|
|
|
|
2019-10-16 06:45:58 +08:00
|
|
|
.. versionadded:: 5.1
|
|
|
|
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2019-07-09 00:00:32 +08:00
|
|
|
Continue on failure
|
|
|
|
-------------------
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
By default, pytest would report only the first failure for a given doctest. If
|
2019-02-13 23:08:49 +08:00
|
|
|
you want to continue the test even when you have failures, do:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2018-02-24 12:42:22 +08:00
|
|
|
|
2019-02-15 20:09:37 +08:00
|
|
|
pytest --doctest-modules --doctest-continue-on-failure
|
2018-02-24 12:42:22 +08:00
|
|
|
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
Output format
|
|
|
|
-------------
|
|
|
|
|
|
|
|
You can change the diff output format on failure for your doctests
|
|
|
|
by using one of standard doctest modules format in options
|
|
|
|
(see :data:`python:doctest.REPORT_UDIFF`, :data:`python:doctest.REPORT_CDIFF`,
|
|
|
|
:data:`python:doctest.REPORT_NDIFF`, :data:`python:doctest.REPORT_ONLY_FIRST_FAILURE`):
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
pytest --doctest-modules --doctest-report none
|
|
|
|
pytest --doctest-modules --doctest-report udiff
|
|
|
|
pytest --doctest-modules --doctest-report cdiff
|
|
|
|
pytest --doctest-modules --doctest-report ndiff
|
|
|
|
pytest --doctest-modules --doctest-report only_first_failure
|
|
|
|
|
|
|
|
|
|
|
|
pytest-specific features
|
|
|
|
------------------------
|
2018-02-09 06:08:44 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
Some features are provided to make writing doctests easier or with better integration with
|
|
|
|
your existing test suite. Keep in mind however that by using those features you will make
|
|
|
|
your doctests incompatible with the standard ``doctests`` module.
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
Using fixtures
|
|
|
|
^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
It is possible to use fixtures using the ``getfixture`` helper:
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
# content of example.rst
|
2021-03-14 03:22:54 +08:00
|
|
|
>>> tmp = getfixture('tmp_path')
|
2019-04-28 22:54:29 +08:00
|
|
|
>>> ...
|
|
|
|
>>>
|
|
|
|
|
2020-10-02 21:39:15 +08:00
|
|
|
Note that the fixture needs to be defined in a place visible by pytest, for example, a `conftest.py`
|
2020-07-15 07:20:23 +08:00
|
|
|
file or plugin; normal python files containing docstrings are not normally scanned for fixtures
|
|
|
|
unless explicitly configured by :confval:`python_files`.
|
|
|
|
|
|
|
|
Also, the :ref:`usefixtures <usefixtures>` mark and fixtures marked as :ref:`autouse <autouse>` are supported
|
2019-04-28 22:54:29 +08:00
|
|
|
when executing text doctest files.
|
|
|
|
|
|
|
|
|
|
|
|
.. _`doctest_namespace`:
|
|
|
|
|
|
|
|
'doctest_namespace' fixture
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2016-03-03 10:37:51 +08:00
|
|
|
|
2016-03-02 20:43:57 +08:00
|
|
|
The ``doctest_namespace`` fixture can be used to inject items into the
|
|
|
|
namespace in which your doctests run. It is intended to be used within
|
|
|
|
your own fixtures to provide the tests that use them with context.
|
|
|
|
|
|
|
|
``doctest_namespace`` is a standard ``dict`` object into which you
|
2019-08-07 07:20:06 +08:00
|
|
|
place the objects you want to appear in the doctest namespace:
|
2016-03-02 20:43:57 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
2016-03-02 20:43:57 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import numpy
|
2019-08-07 04:34:58 +08:00
|
|
|
|
|
|
|
|
2016-03-02 20:43:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2016-03-02 21:02:15 +08:00
|
|
|
def add_np(doctest_namespace):
|
2019-08-07 04:34:58 +08:00
|
|
|
doctest_namespace["np"] = numpy
|
2016-03-02 20:43:57 +08:00
|
|
|
|
2019-08-07 07:20:06 +08:00
|
|
|
which can then be used in your doctests directly:
|
2016-03-02 20:43:57 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
2016-03-02 20:43:57 +08:00
|
|
|
|
|
|
|
# content of numpy.py
|
|
|
|
def arange():
|
|
|
|
"""
|
|
|
|
>>> a = np.arange(10)
|
|
|
|
>>> len(a)
|
|
|
|
10
|
|
|
|
"""
|
|
|
|
pass
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2018-12-05 17:25:52 +08:00
|
|
|
Note that like the normal ``conftest.py``, the fixtures are discovered in the directory tree conftest is in.
|
|
|
|
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
|
|
|
|
Fixtures will not be discovered in a sibling directory tree!
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2020-11-28 23:47:02 +08:00
|
|
|
Skipping tests
|
|
|
|
^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
For the same reasons one might want to skip normal tests, it is also possible to skip
|
|
|
|
tests inside doctests.
|
|
|
|
|
|
|
|
To skip a single check inside a doctest you can use the standard
|
2021-08-26 22:05:03 +08:00
|
|
|
:data:`doctest.SKIP` directive:
|
2020-11-28 23:47:02 +08:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def test_random(y):
|
|
|
|
"""
|
|
|
|
>>> random.random() # doctest: +SKIP
|
|
|
|
0.156231223
|
|
|
|
|
|
|
|
>>> 1 + 1
|
|
|
|
2
|
|
|
|
"""
|
2016-07-23 21:30:06 +08:00
|
|
|
|
2020-11-28 23:47:02 +08:00
|
|
|
This will skip the first check, but not the second.
|
|
|
|
|
|
|
|
pytest also allows using the standard pytest functions :func:`pytest.skip` and
|
|
|
|
:func:`pytest.xfail` inside doctests, which might be useful because you can
|
|
|
|
then skip/xfail tests based on external conditions:
|
2019-02-13 23:08:49 +08:00
|
|
|
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: text
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2019-04-28 22:54:29 +08:00
|
|
|
>>> import sys, pytest
|
|
|
|
>>> if sys.platform.startswith('win'):
|
|
|
|
... pytest.skip('this doctest does not work on Windows')
|
|
|
|
...
|
2020-11-28 23:47:02 +08:00
|
|
|
>>> import fcntl
|
|
|
|
>>> ...
|
|
|
|
|
|
|
|
However using those functions is discouraged because it reduces the readability of the
|
|
|
|
docstring.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
:func:`pytest.skip` and :func:`pytest.xfail` behave differently depending
|
|
|
|
if the doctests are in a Python file (in docstrings) or a text file containing
|
|
|
|
doctests intermingled with text:
|
|
|
|
|
|
|
|
* Python modules (docstrings): the functions only act in that specific docstring,
|
|
|
|
letting the other docstrings in the same module execute as normal.
|
|
|
|
|
|
|
|
* Text files: the functions will skip/xfail the checks for the rest of the entire
|
|
|
|
file.
|
2020-12-05 02:24:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
Alternatives
|
|
|
|
------------
|
|
|
|
|
|
|
|
While the built-in pytest support provides a good set of functionalities for using
|
|
|
|
doctests, if you use them extensively you might be interested in those external packages
|
|
|
|
which add many more features, and include pytest integration:
|
|
|
|
|
|
|
|
* `pytest-doctestplus <https://github.com/astropy/pytest-doctestplus>`__: provides
|
|
|
|
advanced doctest support and enables the testing of reStructuredText (".rst") files.
|
|
|
|
|
|
|
|
* `Sybil <https://sybil.readthedocs.io>`__: provides a way to test examples in
|
|
|
|
your documentation by parsing them from the documentation source and evaluating
|
|
|
|
the parsed examples as part of your normal test run.
|