15.1 Using docstrings for testing
Good Python includes docstrings inside every module, class, function, and method. Many tools can create useful, informative documentation from docstrings. Refer back to the Writing clear documentation strings with RST markup recipe in Chapter 3 for an example of how to create docstrings.
One important element of a docstring is a concrete example. The examples provided in docstrings can become unit-test cases that are exercised by Python’s doctest tool.
In this recipe, we’ll look at ways to turn examples into proper automated test cases.
15.1.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 automated tests.
We’ll use a function to compute the binomial coefficient of two numbers. It shows the number of combinations of n things taken in groups...