This extension allows you to test snippets in the documentation in a natural way. It works by collecting specially-marked up code blocks and running them as doctest tests.
Within one document, test code is partitioned in groups, where each group consists of:
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:
The doctest extension provides four directives. The group argument 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.
A doctest-style code block. You can use standard doctest flags for controlling how actual output is compared with what you give as output. By default, these options are enabled: ELLIPSIS (allowing you to put ellipses in the expected output that match anything in the actual output), IGNORE_EXCEPTION_DETAIL (not comparing tracebacks), DONT_ACCEPT_TRUE_FOR_1 (by default, doctest accepts “True” in the output where “1” is given – this is a relic of pre-Python 2.2 times).
This directive supports two options:
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.).
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.
A code block for a code-output-style test.
This directive supports one option:
The corresponding output for the last testcode block.
This directive supports two options:
Example:
.. testoutput::
:hide:
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE
Output text.
The following is an example for the usage of the directives. The test via doctest and the test via testcode and testoutput are completely equivalent.
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:
.. testcode::
parrot.voom(3000)
This would output:
.. testoutput::
This parrot wouldn't voom if you put 3000 volts through it!
There are also these config values for customizing the doctest extension:
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.
バージョン 0.6 で追加.
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.
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.
(Note that no special :: is needed to introduce the block; docutils recognizes it from the leading >>>. Also, no additional indentation is necessary, though it doesn’t hurt.)
If this value is left at its default value, the above snippet is interpreted by the doctest builder exactly like the following:
Some documentation text.
.. doctest::
>>> print 1
1
Some more documentation text.
This feature makes it easy for you to test doctests in docstrings included with the autodoc extension without marking them up with a special directive.
Note though that you can’t have blank lines in reST doctest blocks. They will be interpreted as one block ending and another one starting. Also, removal of <BLANKLINE> and # doctest: options only works in doctest blocks.