The document test module in Python

Source: Internet
Author: User
Tags abs

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 = (' <=abc) def ', ' abcdef ') >>> (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


1 $ python

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 ********************************************************************** File "", line 7, in __main__. Dict Failed example:d1.x Exception raised:traceback (most recent call last): ... Attributeerror: ' Dict ' object has no attribute ' x ' ****************************************************************** File "", 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.


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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.