Testing with doctest
Explore how to test Python functions using the doctest module by embedding tests within docstrings or separate files. Understand running tests from the terminal, inside modules, and using verbose output to ensure your code performs as expected.
We'll cover the following...
The doctest module will search for pieces of text in your code that resemble interactive Python sessions. It will then execute those sessions to verify that they work exactly as written. This means that if you wrote an example in a docstring that showed the output with a trailing space or tab, then the actual output of the function has to have that trailing whitespace too. Most of the time, the docstring is where you will want to put your tests. The following aspects of doctest will be covered:
- How to run doctest from the terminal
- How to use doctest inside a module
- How to run a doctest from a separate file
Let’s get started!
Running doctest via the Terminal
We will start by creating a really simple function ...