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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.