Introduction and Analysis of the Python test suite

Source: Internet
Author: User

The Python test suite has the most abundant and powerful class libraries in the scripting language and is sufficient to support most daily applications. Its name comes from a comedy. Perhaps those who initially designed the Language Python did not expect that today Python will be widely used in industry and scientific research.

In a long document, a Python prompt >>> consisting of three greater than signs is a very obvious sign, indicating what should happen on the Python prompt. As you can see in the first article in this series, this can appear in a separate text file as a document:

 
 
  1. Doctest for truth and falsehood  
  2. -------------------------------  
  3.  
  4. The truth values in Python, named "True" and "False",  
  5. are equivalent to the Boolean numbers one and zero.  
  6.  
  7. >>> True == 1  
  8. True  
  9. >>> False == 0  
  10. True 

This description can also appear in the docstring of the module, class, or function in the source code:

 
 
  1. def count_vowels(s):  
  2.     """Count the number of vowels in a string.  
  3.  
  4.     >>> count_vowels('aardvark')  
  5.     3  
  6.     >>> count_vowels('THX')  
  7.     0  
  8.  
  9.     """  
  10.     return len( c for c in s if c in 'aeiou') 

When these tests appear in a text file, such as the first example), the file is called docfile. When they appear in docstring in Python source code, such as the second example), they are called doctest. Because docfile and doctest are common methods for writing documents used for testing, and this can also indicate when documents are outdated), py. test and nose support them directly.

Zope. testing users must use the DocTestSuite class in the standard doctest module to manually create Python test cases for each file ). Like the rules used to search for test Modules, The py. test Framework supports doctest in a fixed process and cannot be configured. This standardizes projects, but limits the flexibility of projects.

If its Python test suite is enabled, it will be included in the document string of all Python modules, including the module name that does not contain the test module. search for doctest in all text files ending with the txt extension.

If the-p restdoc plug-in is enabled, not only does the execution of all doctest and py. test in the. txt file require that each. txt file in the project be a valid Restructured Text file. If they cause parsing errors, py. test issues a warning. You can also use the command line option to let the plug-in check the specified URL in the document, and then generate the HTML version of each. txt text file.

Doctest-tests is the option with minimal interference. It only requires nose to search for doctest in the docstring of the tested module that has been checked. The with-doctest option has a greater impact. It requires nose to search for all modules whose general modules are not tests but contain general code), find and run doctest in their docstring.

Finally, -- doctest-extension allows most developers I know to specify the file extension. txt,. rst, or. doctest ). This requires nose to read all text files with the specified extension in the project, run and check all Python Test suites it finds.

Although py. test and nose have similar feature sets in this respect, I prefer the nose method. I like to use a non-standard. rst extension for all Restructured Text files so that the Text editor can recognize them and apply special syntaxes to them for highlighting.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.