Doctest
Encyclopedia
doctest is a module included in the Python
programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstring
s.
When using the Python shell, the primary prompt: >>> , is followed by new commands. The secondary prompt: ... , is used when continuing commands on multiple lines; and the result of executing the command is expected on following lines.
A blank line, or another line starting with the primary prompt is seen as the end of the output from the command.
The doctest module looks for such sequences of prompts in a docstring, re-executes the extracted command and checks the output against the output of the command given in the docstrings test example.
The default action when running doctests is for no output to be shown when tests pass. This can be modified by options to the doctest runner. In addition, doctest has been integrated with the Python unit test module allowing doctests to be run as standard unittest testcases. Unittest testcase runners allow more options when running tests such as the reporting of test statistics such as tests passed, and failed.
A program file can be made to contain the documentation, tests, as well as the code and the tests easily verified against the code. This allows code, tests, and documentation to evolve together.
On the basis of the output of Python's interactive interpreter, text can be mixed with tests that exercise the library, showing expected results.
In the second example, more features of doctest are shown, together with their explanation.
Example three is set up to run all doctests in a file when the file is run, but when imported as a module, the tests will not be run.
This is just an example of what a README text looks like that can be used with
the doctest.DocFileSuite function from Python's doctest module.
Normally, the README file would explain the API of the module, like this:
>>> a = 1
>>> b = 2
>>> a + b
3
Notice, that we just demonstrated how to add two numbers in Python, and
what the result will look like.
A special option allows you to be somewhat fuzzy about your examples:
>>> o = object
>>> o # doctest: +ELLIPSIS
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstring
Docstring
In programming, a docstring is a string literal specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, or even specifically formatted comments like Javadoc documentation, docstrings are not stripped from the source...
s.
Implementation specifics
doctest makes innovative use of the following Python capabilities:- docstringDocstringIn programming, a docstring is a string literal specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, or even specifically formatted comments like Javadoc documentation, docstrings are not stripped from the source...
s - The Python interactive shell, (both command line and the included idle application).
- Python introspection
When using the Python shell, the primary prompt: >>> , is followed by new commands. The secondary prompt: ... , is used when continuing commands on multiple lines; and the result of executing the command is expected on following lines.
A blank line, or another line starting with the primary prompt is seen as the end of the output from the command.
The doctest module looks for such sequences of prompts in a docstring, re-executes the extracted command and checks the output against the output of the command given in the docstrings test example.
The default action when running doctests is for no output to be shown when tests pass. This can be modified by options to the doctest runner. In addition, doctest has been integrated with the Python unit test module allowing doctests to be run as standard unittest testcases. Unittest testcase runners allow more options when running tests such as the reporting of test statistics such as tests passed, and failed.
Literate Programming and Doctests
Although doctest does not allow a Python program to be embedded in narrative text, it does allow for verifiable examples to be embedded in docstrings, where the docstrings can contain other text. Docstrings can in turn be extracted from program files to generate documentation in other formats such as HTML or PDF.A program file can be made to contain the documentation, tests, as well as the code and the tests easily verified against the code. This allows code, tests, and documentation to evolve together.
Documenting libraries by example
Doctests are well suited to provide an introduction to a library by demonstrating how the API is used.On the basis of the output of Python's interactive interpreter, text can be mixed with tests that exercise the library, showing expected results.
Examples
Example one shows how narrative text can be interspersed with testable examples in a docstring.In the second example, more features of doctest are shown, together with their explanation.
Example three is set up to run all doctests in a file when the file is run, but when imported as a module, the tests will not be run.
Example 1: A doctest embedded in the docstring of a function
This is just an example of what a README text looks like that can be used with
the doctest.DocFileSuite function from Python's doctest module.
Normally, the README file would explain the API of the module, like this:
>>> a = 1
>>> b = 2
>>> a + b
3
Notice, that we just demonstrated how to add two numbers in Python, and
what the result will look like.
A special option allows you to be somewhat fuzzy about your examples:
>>> o = object
>>> o # doctest: +ELLIPSIS