Книга: Practical Programming, Fourth Edition
Назад: Defining Your Modules
Дальше: Tips for Grouping Your Functions

Testing Your Code Semiautomatically

In , we introduced the Function Design Recipe (FDR). Following the FDR, the docstrings that you write include example function calls.

The last step of the FDR involves testing the function. Up until now, you have been typing the function calls from the docstrings to the shell (or copying and pasting them) to run them and then comparing the results with what you expect to make sure they match.

Test your programs using the module doctest

Python has a module called doctest that allows you to run the tests included in docstrings all at once. It reports on whether the function calls return what you expect. You will use doctest to run the tests from the temperature_program module from :

 Python 3.14.0b4 (main, Jul 9 2025, 09:00:21) [GCC 11.4.0] on linux
 Enter "help" below or click "Help" above for more information.
 >>>
 ===================== RESTART; /tmp/baking.py ==================
 Enter the temperature in degrees Fahrenheit: 500
 It is above freezing.
 >>>​​ ​​import​​ ​​doctest
 >>>​​ ​​doctest.testmod()
 TestResults(failed=0, attempted=3)
 >>>

That message indicates that three tests were conducted and none of them failed. That is, the three function calls in the docstrings were run, and they returned the same value that you expected and stated in the docstring.

Testing vs. Debugging

icon indicating an aside

Testing is often confused with debugging, but they’re not the same. Testing is the process of checking whether your code behaves as expected; it helps you find problems. Debugging comes afterward: it’s the detective work of figuring out what went wrong and how to fix it. In short, testing asks, “Is there a bug?” and debugging answers, “Why is there a bug, and how do I fix it?”

Now let’s see what happens when there is an error in your calculation. Instead of the calculation you’ve been using, (fahrenheit - 32.0) * 5.0 / 9.0, let’s remove the parentheses: fahrenheit - 32.0 * 5.0 / 9.0.

Here is the result of running doctest on that module:

 Python 3.14.0b4 (main, Jul 9 2025, 09:00:21) [GCC 11.4.0] on linux
 Enter "help" below or click "Help" above for more information.
 >>>
 ===================== RESTART; /tmp/baking.py ==================
 Enter the temperature in degrees Fahrenheit: 500
 It is above freezing.
 >>>​​ ​​import​​ ​​doctest
 >>>​​ ​​doctest.testmod()
 **********************************************************************
 File "/tmp/temperature_program.py", line 5, in __main__.convert_to_celsius
 Failed example:
  convert_to_celsius(75)
 Expected:
  23.88888888888889
 Got:
  57.22222222222222
 **********************************************************************
 1 item had failures:
  1 of 1 in __main__.convert_to_celsius
 ***Test Failed*** 1 failure.
 TestResults(failed=1, attempted=3)
 >>>

The failure message above indicates that the function call convert_to_celsius(75) was expected to return 23.88888888888889, but it returned 57.22222222222222. The other two tests ran and passed.

When a failure occurs, you need to review your code to identify the problem. You should also verify the expected return value listed in the docstring to ensure that it matches both the type contract and function’s description.

Назад: Defining Your Modules
Дальше: Tips for Grouping Your Functions