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 ...

Get Practical Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.