Python code coverage statistical tool coverage. py

Source: Internet
Author: User

Coverage. py is used to count PythonProgramCodeCoverage tool. It is easy to use and supports the final generation of user-friendly HTML reports. In the latest version, the branch coverage function is also provided.

Official Website:

Http://nedbatchelder.com/code/coverage/

Win32 version:

Http://pypi.python.org/pypi/coverage

You can also install the SDK through easy-install:

Easy_install coverage

After installation, a coverage.exe file is installed in the C: \ python25 \ scripts \ (counterfeit file on the cdisk directory. This EXE can basically complete all the functions we need. Run it. If you find that the module is missing, install easy_install first.

Coverage command line

Command Line instructions for use: see http://nedbatchelder.com/code/coverage/cmd.html

The key parameters are as follows:

1.Run

To execute code coverage rate statistics, you only need to run the Statistical Code through the coverage run parameter.

$ Coverage run my_program.py arg1 arg2

After running, a coverage statistical result file (data file):. Coverage is automatically generated. If you want to modify the default file name, you only need to set the coverage_file environment variable.

2.Report

With the coverage rate statistical result file, you only need to run the report parameter to view the statistical result in the command.

$ Coverage report
Name privileges ts exec cover
---------------------------------------------
My_program 20 16 80 %
My_module 15 13 86 %
My_other_module 56 50 89 %
---------------------------------------------
Total 91 79 87 %

3.Html

The most handsome and cool function is to generate the HTML test report directly.

$ Coverage html - D covhtml

The generated reports are cool and directly associated with the Code. The overwrite and overwrite codes are highlighted, and sorting is supported. You can preview at this address:

Http://nedbatchelder.com/code/coverage/sample_html/

The effect is as follows:

 

 

4.Combine

All those who have used the code coverage tool know that merging multiple results is crucial. I have been pondering the combine parameter for a long time and it is always unsuccessful to start merging. Later, I finally understood. The merge operation is simple. You only need to put the coverage result data file to be merged in the same directory, and then execute:

Coverage combine

You can. However, in fact, there are requirements for the results files in the directory, the requirement is the file name format, the files to be merged must have the same prefix, A name (usually the machine name) is followed by a number (usually the process ID), for example:

. Coverage. coderzh. 1234
. Coverage. cnblogs. 5678

To facilitate the merge of execution results, when we execute the preceding statistics, a-p parameter is followed by the run parameter to automatically generate a result file that meets the merging conditions.

$ Coverage run - P my_program.py arg1 arg2

After merging, A. Coverage file will be generated, and then HTML will be executed to view the merged report.

OthersErase Annotate DebugParameters are not described.

Coverage API

In addition to the command line, you can also directly call the coverage module in Python code to perform code coverage statistics. The usage is also very simple:

Import coverage

cov = coverage. coverage ()
cov. start ()

# .. run your code ..

cov. stop ()
cov. save ()

coverage constructor can set the name of the result file. use_cache is a function. If use_cache (0) is set, the result file is not read or written on the hard disk. If you want to merge the result data, you must set use_cache (1 ).

coverage provides some useful functions, such as exclude (excluding statistics code), html_report (generating HTML reports ), Report (console output result)

the next article describes how to test Django applications, write your own test runner to execute code coverage statistics.

Related Article

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.