Good Python includes docstrings inside every module, class, function, and method. Many tools can create useful, informative documentation from the docstrings.
One important element of a docstring is an example. The example becomes a kind of unit test case. An example often fits the GIVEN-WHEN-THEN model of testing because it shows a unit, a request, and a response.
How can we turn examples into proper test cases?
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 tests.
Here's a simple function that computes the binomial coefficient of two numbers. It shows the number of combinations of n things taken in groups of size k. For example, how many ways a 52-card deck can be dealt into 5-card hands is computed like this:
This defines a small Python function that we can write like this:
from math import factorial def binom(n: int, k...