Using doctest to define test cases

The doctest module provides us with a way to validate documentation strings. In addition to docstrings in the code, any document with a Python REPL-style response can be tested via doctest. This will combine the documentation for modules, classes, functions, and test cases into one tidy package.

doctest cases are written into docstrings. A doctest case shows us the interactive Python prompt, >>>; statements; and the expected responses. The doctest module contains an application that looks for these examples in docstrings. It runs the given examples and compares the expected results shown in the docstrings with the actual outputs.

With the careful design of an API, we can create a class that can be used interactively. ...

Get Mastering Object-Oriented Python - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.