Testing Your Code Semiautomatically
In Designing New Functions: A Recipe, we introduced the function design recipe (FDR). Following the FDR, the docstrings that we write include example function calls.
The last step of the FDR involves testing the function. Up until now, we have been typing the function calls from the docstrings to the shell (or copying and pasting them) to run them and then have been comparing the results with what we expect to make sure they match.
Python has a module called doctest that allows us to run the tests that we include in docstrings all at once. It reports on whether the function calls return what we expect. (See Figure 4, The doctest Module Running the Tests from Module temperature_program.) We will use ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access