Testing functions that raise exceptions
Good Python includes docstrings inside every module, class, function, and method. Many tools can create useful, informative documentation from these docstrings.
One important element of a docstring is an example. The example becomes a kind of unit test case. Doctest does simple, literal matching of the expected output against the actual output.
When an example raises an exception, though, the traceback messages from Python are not always identical. It may include object ID values that change or module line numbers which may vary slightly depending on the context in which the test is executed. The literal matching rules for doctest aren't appropriate when exceptions are involved.
How can we turn exception processing and the resulting traceback messages into proper test cases?
Getting ready
We'll look at a simple function definition as well as a simple class definition. Each of these will include docstrings that include examples which can be used as formal...