2008-03-21 09:06:04 -05:00
|
|
|
.. highlight:: rest
|
|
|
|
|
2008-03-12 16:37:22 -05:00
|
|
|
:mod:`sphinx.ext.doctest` -- Test snippets in the documentation
|
|
|
|
===============================================================
|
|
|
|
|
|
|
|
.. module:: sphinx.ext.doctest
|
|
|
|
:synopsis: Test snippets in the documentation.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
.. index:: pair: automatic; testing
|
|
|
|
single: doctest
|
|
|
|
pair: testing; snippets
|
|
|
|
|
|
|
|
|
2020-02-09 17:59:39 -06:00
|
|
|
It is often helpful to include snippets of code in your documentation and
|
2020-02-09 20:12:52 -06:00
|
|
|
demonstrate the results of executing them. But it is important to ensure that
|
2020-02-09 17:59:39 -06:00
|
|
|
the documentation stays up-to-date with the code.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2020-02-09 17:59:39 -06:00
|
|
|
This extension allows you to test such code snippets in the documentation in
|
2020-02-09 20:12:52 -06:00
|
|
|
a natural way. If you mark the code blocks as shown here, the ``doctest``
|
2020-02-09 17:59:39 -06:00
|
|
|
builder will collect them and run them as doctest tests.
|
|
|
|
|
|
|
|
Within each document, you can assign each snippet to a *group*. Each group
|
2008-03-21 09:06:04 -05:00
|
|
|
consists of:
|
|
|
|
|
|
|
|
* zero or more *setup code* blocks (e.g. importing the module to test)
|
|
|
|
* one or more *test* blocks
|
|
|
|
|
|
|
|
When building the docs with the ``doctest`` builder, groups are collected for
|
|
|
|
each document and run one after the other, first executing setup code blocks,
|
|
|
|
then the test blocks in the order they appear in the file.
|
|
|
|
|
|
|
|
There are two kinds of test blocks:
|
|
|
|
|
|
|
|
* *doctest-style* blocks mimic interactive sessions by interleaving Python code
|
|
|
|
(including the interpreter prompt) and output.
|
|
|
|
|
|
|
|
* *code-output-style* blocks consist of an ordinary piece of Python code, and
|
|
|
|
optionally, a piece of output for that code.
|
|
|
|
|
2016-05-01 13:29:15 -05:00
|
|
|
|
|
|
|
Directives
|
|
|
|
----------
|
|
|
|
|
|
|
|
The *group* argument below is interpreted as follows: if it is empty, the block
|
|
|
|
is assigned to the group named ``default``. If it is ``*``, the block is
|
|
|
|
assigned to all groups (including the ``default`` group). Otherwise, it must be
|
|
|
|
a comma-separated list of group names.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2010-04-17 03:39:51 -05:00
|
|
|
.. rst:directive:: .. testsetup:: [group]
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
A setup code block. This code is not shown in the output for other builders,
|
|
|
|
but executed before the doctests of the group(s) it belongs to.
|
|
|
|
|
2008-03-22 08:07:22 -05:00
|
|
|
|
2011-01-03 14:20:29 -06:00
|
|
|
.. rst:directive:: .. testcleanup:: [group]
|
|
|
|
|
|
|
|
A cleanup code block. This code is not shown in the output for other
|
|
|
|
builders, but executed after the doctests of the group(s) it belongs to.
|
|
|
|
|
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
|
|
|
|
2010-04-17 03:39:51 -05:00
|
|
|
.. rst:directive:: .. doctest:: [group]
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2016-04-21 05:30:39 -05:00
|
|
|
A doctest-style code block. You can use standard :mod:`doctest` flags for
|
|
|
|
controlling how actual output is compared with what you give as output. The
|
|
|
|
default set of flags is specified by the :confval:`doctest_default_flags`
|
|
|
|
configuration variable.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2017-01-15 03:27:34 -06:00
|
|
|
This directive supports three options:
|
2008-03-22 08:07:22 -05:00
|
|
|
|
|
|
|
* ``hide``, a flag option, hides the doctest block in other builders. By
|
|
|
|
default it is shown as a highlighted doctest block.
|
|
|
|
|
|
|
|
* ``options``, a string option, can be used to give a comma-separated list of
|
|
|
|
doctest flags that apply to each example in the tests. (You still can give
|
|
|
|
explicit flags per example, with doctest comments, but they will show up in
|
|
|
|
other builders too.)
|
|
|
|
|
2017-01-15 03:27:34 -06:00
|
|
|
* ``pyversion``, a string option, can be used to specify the required Python
|
|
|
|
version for the example to be tested. For instance, in the following case
|
2018-12-25 12:10:25 -06:00
|
|
|
the example will be tested only for Python versions greater than 3.3::
|
2017-01-15 03:27:34 -06:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
:pyversion: > 3.3
|
2017-01-16 03:53:46 -06:00
|
|
|
|
2018-01-07 20:59:50 -06:00
|
|
|
The following operands are supported:
|
|
|
|
|
|
|
|
* ``~=``: Compatible release clause
|
|
|
|
* ``==``: Version matching clause
|
|
|
|
* ``!=``: Version exclusion clause
|
|
|
|
* ``<=``, ``>=``: Inclusive ordered comparison clause
|
|
|
|
* ``<``, ``>``: Exclusive ordered comparison clause
|
|
|
|
* ``===``: Arbitrary equality clause.
|
|
|
|
|
|
|
|
``pyversion`` option is followed `PEP-440: Version Specifiers
|
|
|
|
<https://www.python.org/dev/peps/pep-0440/#version-specifiers>`__.
|
2017-01-15 14:07:44 -06:00
|
|
|
|
2017-01-26 04:20:24 -06:00
|
|
|
.. versionadded:: 1.6
|
2017-01-15 03:27:34 -06:00
|
|
|
|
2018-01-07 20:59:50 -06:00
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
Supported PEP-440 operands and notations
|
|
|
|
|
2008-03-22 16:10:35 -05:00
|
|
|
Note that like with standard doctests, you have to use ``<BLANKLINE>`` to
|
|
|
|
signal a blank line in the expected output. The ``<BLANKLINE>`` is removed
|
|
|
|
when building presentation output (HTML, LaTeX etc.).
|
|
|
|
|
2008-03-22 16:21:28 -05:00
|
|
|
Also, you can give inline doctest options, like in doctest::
|
|
|
|
|
|
|
|
>>> datetime.date.now() # doctest: +SKIP
|
|
|
|
datetime.date(2008, 1, 1)
|
|
|
|
|
|
|
|
They will be respected when the test is run, but stripped from presentation
|
|
|
|
output.
|
|
|
|
|
2008-03-22 08:07:22 -05:00
|
|
|
|
2010-04-17 03:39:51 -05:00
|
|
|
.. rst:directive:: .. testcode:: [group]
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
A code block for a code-output-style test.
|
|
|
|
|
2008-03-22 08:07:22 -05:00
|
|
|
This directive supports one option:
|
|
|
|
|
|
|
|
* ``hide``, a flag option, hides the code block in other builders. By
|
|
|
|
default it is shown as a highlighted code block.
|
|
|
|
|
2010-01-02 07:59:27 -06:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Code in a ``testcode`` block is always executed all at once, no matter how
|
|
|
|
many statements it contains. Therefore, output will *not* be generated
|
|
|
|
for bare expressions -- use ``print``. Example::
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
|
|
|
1+1 # this will give no output!
|
|
|
|
print 2+2 # this will give output
|
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
Also, please be aware that since the doctest module does not support
|
|
|
|
mixing regular output and an exception message in the same snippet, this
|
|
|
|
applies to testcode/testoutput as well.
|
|
|
|
|
2008-03-22 08:07:22 -05:00
|
|
|
|
2010-04-17 03:39:51 -05:00
|
|
|
.. rst:directive:: .. testoutput:: [group]
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2010-01-02 07:59:27 -06:00
|
|
|
The corresponding output, or the exception message, for the last
|
2010-04-17 03:39:51 -05:00
|
|
|
:rst:dir:`testcode` block.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
This directive supports two options:
|
|
|
|
|
|
|
|
* ``hide``, a flag option, hides the output block in other builders. By
|
|
|
|
default it is shown as a literal block without highlighting.
|
|
|
|
|
|
|
|
* ``options``, a string option, can be used to give doctest flags
|
|
|
|
(comma-separated) just like in normal doctest blocks.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
2010-01-02 07:59:27 -06:00
|
|
|
.. testcode::
|
|
|
|
|
|
|
|
print 'Output text.'
|
|
|
|
|
2008-03-21 09:06:04 -05:00
|
|
|
.. testoutput::
|
|
|
|
:hide:
|
|
|
|
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE
|
|
|
|
|
|
|
|
Output text.
|
|
|
|
|
|
|
|
The following is an example for the usage of the directives. The test via
|
2014-06-18 10:53:25 -05:00
|
|
|
:rst:dir:`doctest` and the test via :rst:dir:`testcode` and
|
|
|
|
:rst:dir:`testoutput` are equivalent. ::
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
The parrot module
|
|
|
|
=================
|
|
|
|
|
|
|
|
.. testsetup:: *
|
|
|
|
|
|
|
|
import parrot
|
|
|
|
|
|
|
|
The parrot module is a module about parrots.
|
|
|
|
|
|
|
|
Doctest example:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> parrot.voom(3000)
|
|
|
|
This parrot wouldn't voom if you put 3000 volts through it!
|
|
|
|
|
|
|
|
Test-Output example:
|
|
|
|
|
2009-01-10 15:18:18 -06:00
|
|
|
.. testcode::
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
parrot.voom(3000)
|
|
|
|
|
|
|
|
This would output:
|
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
|
|
|
|
This parrot wouldn't voom if you put 3000 volts through it!
|
|
|
|
|
|
|
|
|
2018-08-16 06:01:57 -05:00
|
|
|
Skipping tests conditionally
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
``skipif``, a string option, can be used to skip directives conditionally. This
|
|
|
|
may be useful e.g. when a different set of tests should be run depending on the
|
|
|
|
environment (hardware, network/VPN, optional dependencies or different versions
|
|
|
|
of dependencies). The ``skipif`` option is supported by all of the doctest
|
|
|
|
directives. Below are typical use cases for ``skipif`` when used for different
|
|
|
|
directives:
|
|
|
|
|
|
|
|
- :rst:dir:`testsetup` and :rst:dir:`testcleanup`
|
|
|
|
|
|
|
|
- conditionally skip test setup and/or cleanup
|
|
|
|
- customize setup/cleanup code per environment
|
|
|
|
|
|
|
|
- :rst:dir:`doctest`
|
|
|
|
|
|
|
|
- conditionally skip both a test and its output verification
|
|
|
|
|
|
|
|
- :rst:dir:`testcode`
|
|
|
|
|
|
|
|
- conditionally skip a test
|
|
|
|
- customize test code per environment
|
|
|
|
|
|
|
|
- :rst:dir:`testoutput`
|
|
|
|
|
|
|
|
- conditionally skip output assertion for a skipped test
|
|
|
|
- expect different output depending on the environment
|
|
|
|
|
|
|
|
The value of the ``skipif`` option is evaluated as a Python expression. If the
|
|
|
|
result is a true value, the directive is omitted from the test run just as if
|
|
|
|
it wasn't present in the file at all.
|
|
|
|
|
|
|
|
Instead of repeating an expression, the :confval:`doctest_global_setup`
|
|
|
|
configuration option can be used to assign it to a variable which can then be
|
|
|
|
used instead.
|
|
|
|
|
|
|
|
Here's an example which skips some tests if Pandas is not installed:
|
|
|
|
|
|
|
|
.. code-block:: py
|
|
|
|
:caption: conf.py
|
|
|
|
|
|
|
|
extensions = ['sphinx.ext.doctest']
|
|
|
|
doctest_global_setup = '''
|
|
|
|
try:
|
|
|
|
import pandas as pd
|
|
|
|
except ImportError:
|
|
|
|
pd = None
|
|
|
|
'''
|
|
|
|
|
|
|
|
.. code-block:: rst
|
|
|
|
:caption: contents.rst
|
|
|
|
|
|
|
|
.. testsetup::
|
|
|
|
:skipif: pd is None
|
|
|
|
|
|
|
|
data = pd.Series([42])
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
:skipif: pd is None
|
|
|
|
|
|
|
|
>>> data.iloc[0]
|
|
|
|
42
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
:skipif: pd is None
|
|
|
|
|
|
|
|
print(data.iloc[-1])
|
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
:skipif: pd is None
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
2016-05-01 13:29:15 -05:00
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
The doctest extension uses the following configuration values:
|
2008-03-21 09:06:04 -05:00
|
|
|
|
2016-04-21 03:54:53 -05:00
|
|
|
.. confval:: doctest_default_flags
|
|
|
|
|
2016-04-21 07:17:43 -05:00
|
|
|
By default, these options are enabled:
|
2016-07-07 08:36:44 -05:00
|
|
|
|
2016-04-21 07:17:43 -05:00
|
|
|
- ``ELLIPSIS``, allowing you to put ellipses in the expected output that
|
|
|
|
match anything in the actual output;
|
|
|
|
- ``IGNORE_EXCEPTION_DETAIL``, causing everything following the leftmost
|
|
|
|
colon and any module information in the exception name to be ignored;
|
|
|
|
- ``DONT_ACCEPT_TRUE_FOR_1``, rejecting "True" in the output where "1" is
|
|
|
|
given -- the default behavior of accepting this substitution is a relic of
|
|
|
|
pre-Python 2.2 times.
|
2016-04-21 05:30:39 -05:00
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
2016-04-21 03:54:53 -05:00
|
|
|
|
2008-03-21 09:06:04 -05:00
|
|
|
.. confval:: doctest_path
|
|
|
|
|
|
|
|
A list of directories that will be added to :data:`sys.path` when the doctest
|
|
|
|
builder is used. (Make sure it contains absolute paths.)
|
|
|
|
|
2008-12-15 05:33:13 -06:00
|
|
|
.. confval:: doctest_global_setup
|
|
|
|
|
|
|
|
Python code that is treated like it were put in a ``testsetup`` directive for
|
|
|
|
*every* file that is tested, and for every group. You can use this to
|
|
|
|
e.g. import modules you will always need in your doctests.
|
|
|
|
|
|
|
|
.. versionadded:: 0.6
|
2009-01-10 15:18:18 -06:00
|
|
|
|
2011-01-03 14:20:29 -06:00
|
|
|
.. confval:: doctest_global_cleanup
|
|
|
|
|
|
|
|
Python code that is treated like it were put in a ``testcleanup`` directive
|
|
|
|
for *every* file that is tested, and for every group. You can use this to
|
|
|
|
e.g. remove any temporary files that the tests leave behind.
|
|
|
|
|
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
2008-03-21 09:06:04 -05:00
|
|
|
.. confval:: doctest_test_doctest_blocks
|
|
|
|
|
2008-03-22 08:07:22 -05:00
|
|
|
If this is a nonempty string (the default is ``'default'``), standard reST
|
|
|
|
doctest blocks will be tested too. They will be assigned to the group name
|
|
|
|
given.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
reST doctest blocks are simply doctests put into a paragraph of their own,
|
|
|
|
like so::
|
|
|
|
|
|
|
|
Some documentation text.
|
|
|
|
|
|
|
|
>>> print 1
|
|
|
|
1
|
|
|
|
|
|
|
|
Some more documentation text.
|
|
|
|
|
2009-04-27 10:34:27 -05:00
|
|
|
(Note that no special ``::`` is used to introduce a doctest block; docutils
|
|
|
|
recognizes them from the leading ``>>>``. Also, no additional indentation is
|
|
|
|
used, though it doesn't hurt.)
|
2008-03-22 08:07:22 -05:00
|
|
|
|
2008-03-22 15:47:27 -05:00
|
|
|
If this value is left at its default value, the above snippet is interpreted
|
|
|
|
by the doctest builder exactly like the following::
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
Some documentation text.
|
|
|
|
|
2008-03-22 15:47:27 -05:00
|
|
|
.. doctest::
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
>>> print 1
|
|
|
|
1
|
|
|
|
|
2009-01-10 15:18:18 -06:00
|
|
|
Some more documentation text.
|
2008-03-21 09:06:04 -05:00
|
|
|
|
|
|
|
This feature makes it easy for you to test doctests in docstrings included
|
|
|
|
with the :mod:`~sphinx.ext.autodoc` extension without marking them up with a
|
|
|
|
special directive.
|
2008-03-22 15:47:27 -05:00
|
|
|
|
|
|
|
Note though that you can't have blank lines in reST doctest blocks. They
|
2008-03-22 16:21:28 -05:00
|
|
|
will be interpreted as one block ending and another one starting. Also,
|
|
|
|
removal of ``<BLANKLINE>`` and ``# doctest:`` options only works in
|
2014-06-18 10:53:25 -05:00
|
|
|
:rst:dir:`doctest` blocks, though you may set :confval:`trim_doctest_flags`
|
|
|
|
to achieve that in all code blocks with Python console content.
|