Doctest in Python is a simple yet useful testing method for Python programs that allows test cases to be written within the doc information of a function, which can then be executed using the doctest module.
Abstract
Doctest in Python is a convenient and simple way to test functions. It involves three main steps: importing the doctest module, writing test cases within triple single/double quotes, and using doctest.testmod() to run doctest. Doctest follows the input and output styles of the Python shell, and outputs detailed information only for failed cases by default. However, this can be changed by setting the verbose parameter to True. Doctest can also be run directly from the terminal, and test cases can be put in an independent file. Doctest is an easy-to-use test method that can help ensure a bug-free code.
Bullet points
Doctest is a simple and useful testing method for Python programs.
Test cases can be written within the doc information of a function.
The doctest module is used to execute test cases.
Doctest follows the input and output styles of the Python shell.
The doctest.testmod() function is used to run doctest.
By default, only failed cases output detailed information.
The verbose parameter can be set to True to output full information.
Doctest can be run directly from the terminal.
Test cases can be put in an independent file.
Doctest is an easy-to-use test method that can help ensure a bug-free code.
Doctest is a simple but useful testing method for Python programs. Compared with unit test, doctest doesn’t require an independent script to write test cases. Test cases can just be written in the doc information (contents within triple single/double quotes) of a Python function. Then these cases can test the function by using the doctest module.
The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. (python document)
A Simple Example
Let’s use doctest for a wrong abs() function:
The result is as following:
A simple example of doctest
As the above example shown, the doctest is really a convenient and simple way to test the function.
In a nutshell, there are just 3 steps:
import doctest module
write the test cases within triple single/double quotes using
use doctest.testmod() to run doctest
Note: The doctest strictly follows the input and output styles of the Python shell. Therefore, don’t forget the >>> of every input.
If all test cases are passed, the doctest will print nothing.
More Tips of Doctest
1. To Print More Information
In doctest.testmod , there is an important parameter called verbose .
It’s set to False by default, which means that when running the test, only failed cases will output detailed information. If you would like to get full information of every cases, just set it to True .
if __name__ == '__main__':
doctest.testmod(verbose=True)
2. Another Method to Run Doctest
Actually, we don’t have to write the if __name__ == '__main__': block. We can run doctest directly in the terminal:
$ python -m doctest wrong_abs.py
Would like to set verbose to True ? No problem! 👌
$ python -m doctest -v wrong_abs.py
3. Put Test Cases to A File
Sometimes, it could be a little messy if we put both comments and test cases below the function. No worries, we can put all the test cases to an independent file and still run doctest perfectly.
For example, let’s create a file called test_cases.txt and put “doctest style” test cases into it:
A file to save test cases
Then run the doctest by this command:
python -m doctest test_cases.txt
The result is identical with the previous one.
Conclusion
Doctest is an easy-to-use test method for Python programs. Using this technique well can help your get a bug-free code.