Skip to Content
Expert Python Programming - Third Edition
book

Expert Python Programming - Third Edition

by Michał Jaworski, Tarek Ziadé, Cody Jackson
April 2019
Intermediate to advanced
646 pages
16h 48m
English
Packt Publishing
Content preview from Expert Python Programming - Third Edition

doctest

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Expert Python Programming - Fourth Edition

Expert Python Programming - Fourth Edition

Michał Jaworski, Tarek Ziade, Tarek Ziadé

Publisher Resources

ISBN: 9781789808896Other