15.2 Testing functions that raise exceptions
Python permits docstrings inside packages, modules, classes, functions, and methods. A good docstring should contain an example of how the feature is used. The example may need to include common exceptions, too. There’s one complicating factor, however, to including exceptions.
When an exception is raised, the traceback messages created by Python are not completely predictable. The message may include object ID values that are impossible to predict or module line numbers that may vary slightly depending on the context in which the test is executed. The general matching rules for doctest compare expected and actual results precisely. In this recipe, we’ll look at additional techniques to add flexibility.
15.2.1 Getting ready
We’ll look at a small function definition as well as a class definition. Each of these will contain docstrings that include examples that can be used as...