April 2019
Intermediate to advanced
646 pages
16h 48m
English
doctest is a module that extracts test snippets in the form of interactive prompt sessions from docstrings or text files, and replays them to check whether the example output is the same as the real one.
For instance, the text file with the following content could be run as a test:
Check addition of integers works as expected::
>>> 1 + 1
2
Let's assume that this documentation file is stored in the filesystem under the test.rst name. The doctest module provides some functions to extract and run the tests from such documentation files, as follows:
>>> import doctest
>>> doctest.testfile('test.rst', verbose=True) Trying: 1 + 1 Expecting: 2 ok 1 items passed all tests: 1 tests in test.rst 1 tests in 1 items. 1 passed and 0 failed. Test ...