|
|
|
@ -1,3 +1,5 @@ |
|
|
|
:keepdoctest: |
|
|
|
|
|
|
|
:mod:`doctest` --- Test interactive Python examples |
|
|
|
=================================================== |
|
|
|
|
|
|
|
@ -318,7 +320,8 @@ The fine print: |
|
|
|
Tabs in output generated by the tested code are not modified. Because any |
|
|
|
hard tabs in the sample output *are* expanded, this means that if the code |
|
|
|
output includes hard tabs, the only way the doctest can pass is if the |
|
|
|
:const:`NORMALIZE_WHITESPACE` option or directive is in effect. |
|
|
|
:const:`NORMALIZE_WHITESPACE` option or :ref:`directive <doctest-directives>` |
|
|
|
is in effect. |
|
|
|
Alternatively, the test can be rewritten to capture the output and compare it |
|
|
|
to an expected value as part of the test. This handling of tabs in the |
|
|
|
source was arrived at through trial and error, and has proven to be the least |
|
|
|
@ -483,15 +486,16 @@ Some details you should read once, but won't need to remember: |
|
|
|
SyntaxError: invalid syntax |
|
|
|
|
|
|
|
|
|
|
|
.. _option-flags-and-directives: |
|
|
|
.. _doctest-options: |
|
|
|
|
|
|
|
Option Flags and Directives |
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
|
|
Option Flags |
|
|
|
^^^^^^^^^^^^ |
|
|
|
|
|
|
|
A number of option flags control various aspects of doctest's behavior. |
|
|
|
Symbolic names for the flags are supplied as module constants, which can be |
|
|
|
or'ed together and passed to various functions. The names can also be used in |
|
|
|
doctest directives (see below). |
|
|
|
:ref:`doctest directives <doctest-directives>`. |
|
|
|
|
|
|
|
The first group of options define test semantics, controlling aspects of how |
|
|
|
doctest decides whether actual output matches an example's expected output: |
|
|
|
@ -545,14 +549,14 @@ doctest decides whether actual output matches an example's expected output: |
|
|
|
:exc:`TypeError` is raised. |
|
|
|
|
|
|
|
It will also ignore the module name used in Python 3 doctest reports. Hence |
|
|
|
both these variations will work regardless of whether the test is run under |
|
|
|
Python 2.7 or Python 3.2 (or later versions): |
|
|
|
both of these variations will work with the flag specified, regardless of |
|
|
|
whether the test is run under Python 2.7 or Python 3.2 (or later versions):: |
|
|
|
|
|
|
|
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
|
|
|
>>> raise CustomError('message') |
|
|
|
Traceback (most recent call last): |
|
|
|
CustomError: message |
|
|
|
|
|
|
|
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
|
|
|
>>> raise CustomError('message') |
|
|
|
Traceback (most recent call last): |
|
|
|
my_module.CustomError: message |
|
|
|
|
|
|
|
@ -562,15 +566,16 @@ doctest decides whether actual output matches an example's expected output: |
|
|
|
exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details |
|
|
|
from Python 2.3 is also the only clear way to write a doctest that doesn't |
|
|
|
care about the exception detail yet continues to pass under Python 2.3 or |
|
|
|
earlier (those releases do not support doctest directives and ignore them |
|
|
|
as irrelevant comments). For example, :: |
|
|
|
earlier (those releases do not support :ref:`doctest directives |
|
|
|
<doctest-directives>` and ignore them as irrelevant comments). For example:: |
|
|
|
|
|
|
|
>>> (1, 2)[3] = 'moo' #doctest: +IGNORE_EXCEPTION_DETAIL |
|
|
|
>>> (1, 2)[3] = 'moo' |
|
|
|
Traceback (most recent call last): |
|
|
|
File "<stdin>", line 1, in ? |
|
|
|
TypeError: object doesn't support item assignment |
|
|
|
|
|
|
|
passes under Python 2.3 and later Python versions, even though the detail |
|
|
|
passes under Python 2.3 and later Python versions with the flag specified, |
|
|
|
even though the detail |
|
|
|
changed in Python 2.4 to say "does not" instead of "doesn't". |
|
|
|
|
|
|
|
.. versionchanged:: 3.2 |
|
|
|
@ -632,9 +637,30 @@ The second group of options controls how test failures are reported: |
|
|
|
|
|
|
|
A bitmask or'ing together all the reporting flags above. |
|
|
|
|
|
|
|
"Doctest directives" may be used to modify the option flags for individual |
|
|
|
examples. Doctest directives are expressed as a special Python comment |
|
|
|
following an example's source code: |
|
|
|
|
|
|
|
There is also a way to register new option flag names, though this isn't |
|
|
|
useful unless you intend to extend :mod:`doctest` internals via subclassing: |
|
|
|
|
|
|
|
|
|
|
|
.. function:: register_optionflag(name) |
|
|
|
|
|
|
|
Create a new option flag with a given name, and return the new flag's integer |
|
|
|
value. :func:`register_optionflag` can be used when subclassing |
|
|
|
:class:`OutputChecker` or :class:`DocTestRunner` to create new options that are |
|
|
|
supported by your subclasses. :func:`register_optionflag` should always be |
|
|
|
called using the following idiom:: |
|
|
|
|
|
|
|
MY_FLAG = register_optionflag('MY_FLAG') |
|
|
|
|
|
|
|
|
|
|
|
.. _doctest-directives: |
|
|
|
|
|
|
|
Directives |
|
|
|
^^^^^^^^^^ |
|
|
|
|
|
|
|
Doctest directives may be used to modify the :ref:`option flags |
|
|
|
<doctest-options>` for an individual example. Doctest directives are |
|
|
|
special Python comments following an example's source code: |
|
|
|
|
|
|
|
.. productionlist:: doctest |
|
|
|
directive: "#" "doctest:" `directive_options` |
|
|
|
@ -650,43 +676,28 @@ above. |
|
|
|
An example's doctest directives modify doctest's behavior for that single |
|
|
|
example. Use ``+`` to enable the named behavior, or ``-`` to disable it. |
|
|
|
|
|
|
|
.. note:: |
|
|
|
Due to an `unfortunate limitation`_ of our current documentation |
|
|
|
publishing process, syntax highlighting has been disabled in the examples |
|
|
|
below in order to ensure the doctest directives are correctly displayed. |
|
|
|
|
|
|
|
.. _unfortunate limitation: http://bugs.python.org/issue12947 |
|
|
|
For example, this test passes:: |
|
|
|
|
|
|
|
For example, this test passes: |
|
|
|
|
|
|
|
.. code-block:: text |
|
|
|
|
|
|
|
>>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE |
|
|
|
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE |
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
|
|
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
|
|
|
|
|
|
|
Without the directive it would fail, both because the actual output doesn't have |
|
|
|
two blanks before the single-digit list elements, and because the actual output |
|
|
|
is on a single line. This test also passes, and also requires a directive to do |
|
|
|
so: |
|
|
|
|
|
|
|
.. code-block:: text |
|
|
|
so:: |
|
|
|
|
|
|
|
>>> print(list(range(20))) # doctest: +ELLIPSIS |
|
|
|
[0, 1, ..., 18, 19] |
|
|
|
|
|
|
|
Multiple directives can be used on a single physical line, separated by |
|
|
|
commas: |
|
|
|
|
|
|
|
.. code-block:: text |
|
|
|
commas:: |
|
|
|
|
|
|
|
>>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
|
|
|
[0, 1, ..., 18, 19] |
|
|
|
|
|
|
|
If multiple directive comments are used for a single example, then they are |
|
|
|
combined: |
|
|
|
|
|
|
|
.. code-block:: text |
|
|
|
combined:: |
|
|
|
|
|
|
|
>>> print(list(range(20))) # doctest: +ELLIPSIS |
|
|
|
... # doctest: +NORMALIZE_WHITESPACE |
|
|
|
@ -694,9 +705,7 @@ combined: |
|
|
|
|
|
|
|
As the previous example shows, you can add ``...`` lines to your example |
|
|
|
containing only directives. This can be useful when an example is too long for |
|
|
|
a directive to comfortably fit on the same line: |
|
|
|
|
|
|
|
.. code-block:: text |
|
|
|
a directive to comfortably fit on the same line:: |
|
|
|
|
|
|
|
>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40))) |
|
|
|
... # doctest: +ELLIPSIS |
|
|
|
@ -708,20 +717,6 @@ usually the only meaningful choice. However, option flags can also be passed to |
|
|
|
functions that run doctests, establishing different defaults. In such cases, |
|
|
|
disabling an option via ``-`` in a directive can be useful. |
|
|
|
|
|
|
|
There's also a way to register new option flag names, although this isn't useful |
|
|
|
unless you intend to extend :mod:`doctest` internals via subclassing: |
|
|
|
|
|
|
|
|
|
|
|
.. function:: register_optionflag(name) |
|
|
|
|
|
|
|
Create a new option flag with a given name, and return the new flag's integer |
|
|
|
value. :func:`register_optionflag` can be used when subclassing |
|
|
|
:class:`OutputChecker` or :class:`DocTestRunner` to create new options that are |
|
|
|
supported by your subclasses. :func:`register_optionflag` should always be |
|
|
|
called using the following idiom:: |
|
|
|
|
|
|
|
MY_FLAG = register_optionflag('MY_FLAG') |
|
|
|
|
|
|
|
|
|
|
|
.. _doctest-warnings: |
|
|
|
|
|
|
|
|