This article mainly introduces the document test module in Python, the code is based on the python2.x version, the need for friends can refer to the
If you often read the official Python documentation, you can see that many documents have sample code. For example, the RE module brings a lot of sample code:
?
1 2 3 4 |
>>> import re >>> m = Re.search (' <=abc) def ', ' abcdef ') >>> m.group (0) ' Def ' |
You can enter and execute the sample code in a Python interactive environment, and the results are consistent with the sample code in the document.
The code and other instructions can be written in a comment, and then some tools will automatically generate the document. Now that the code itself can be pasted and run directly, is it possible to automatically execute the code written in the comments?
The answer is yes.
When we write a comment, if it's written like this:
?
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
def ABS (n): ' Function to get absolute value of number. Example: >>> ABS (1) 1 >>> abs ( -1) 1 >>> abs (0) 0 ' return n if n >= 0 else (-N) |
It is no doubt more explicit to tell the caller of the function the expected input and output of the function.
Also, the Python built-in document test (Doctest) module can directly extract the code from the annotation and execute the test.
Doctest strictly follow the input and output of the Python interactive command line to determine whether the test results are correct. Only when the test is abnormal, can you use ... Represents a large part of the middle of the annoying output.
Let's use Doctest to test the last written Dict class:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Run Python mydict.py:
?
There is no output. This means that the doctest that we write are all correct. If there is a problem with the program, such as commenting out the __getattr__ () method, then the operation will be an error:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 |
$ python mydict.py ********************************************************************** File "mydict.py", line 7, in __main__. Dict Failed example:d1.x Exception raised:traceback (most recent call last): ... Attributeerror: ' Dict ' object has no attribute ' x ' ****************************************************************** File "mydict.py", line, in __main__. Dict Failed example:d2.c Exception raised:traceback (most recent call last): ... Attributeerror: ' Dict ' object has no attribute ' C ' ****************************************************************** **** |
Notice the last two lines of code. Doctest is not executed when the module is imported normally. Doctest is performed only when the command line is running. So, you don't have to worry that Doctest will execute in a non test environment.
Summary
Doctest is useful, not only for testing, but also for example code. With some document generation tools, you can automatically extract the annotations that contain doctest. When users look at the document, they also see the doctest.