Using coverage tools to count Python unit test coverage

Source: Internet
Author: User

Coverage Introduction

Coverage is a tool for statistical Python code coverage that detects how the test code covers the code being measured. Coverage supports branch coverage statistics and can generate Html/xml reports.

Official Document: http://coverage.readthedocs.org/en/latest/

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

You can also install via Easy_install or PIP: Easy_install coverage.

After installation, tools such as Coverage.exe are generated under the Python27\scripts folder, and the coverage command can be called on the command line for coverage analysis by adding environment variable C:\Python27\Scripts in path.

Coverage using 1. Command line mode

See: http://coverage.readthedocs.io/en/latest/cmd.html

You can use the Help command to view assistance: $ coverage helps

The key commands are as follows:

A . Run

To perform code coverage statistics, you only need to execute the statistics code through the coverage's run parameters.

$ coverage Run test.py arg1 arg2

test.py is the test script, arg1 arg2 is the parameter that the test.py executes. After running, a coverage statistic result file (data file) is automatically generated:. Coverage.

B . Report

With the coverage statistics results file, you can see the results of the statistics in the command by simply running the report parameter.

Stmts/miss represents the total number of statements/statements not executed to

Cover= (Stmts-miss)/stmts

C. html

Generates a test report for HTML.

$ coverage html-d covhtml

Generated reports are directly associated with code, highlighting overrides and uncovered code, and supporting sorting. -d Specifies the HTML folder. You can preview it at this address: http://nedbatchelder.com/code/coverage/sample_html/

The effect is as follows:

Test file:

Highlights covered and uncovered code, covered in green, and not covered in red.

Note: The file run by the coverage run test.py command will count all the files in the project including the test file itself, and the run parameter's sub-parameter-source can specify the file to be counted: $ coverage Run--source=totest.py test.py can only count totest.py files.

D.combine

Merge multiple coverage data files, place the coverage result data files that need to be merged in the same directory, and then perform coverage combine. However, the format of the result file name in the directory requires that the file to be merged must have the same prefix, followed by a name (usually the machine name), followed by a number (usually the process id), For example:. coverage.75fec5d4b3adaaa.14632.213308

To facilitate the merging of results, we followed the run parameter with a-p parameter, which automatically generates a result file that conforms to the merge condition when we performed the statistics earlier.

E.run parameter Sub-parameter 1:--branch Statistic branch coverage

If a branch in your code might jump to more than one row, coverage.py will track whether each branch has a jump to it.

$ coverage Run--branch test.py

Branch/brpart = number of branches/branches not executed

F.run parameter sub-parameter 2:--parallel-mode

$ coverage Run--parallel-mode test.py

Enable coverage to monitor the coverage of the code sub-process under test, if the code being tested is multi-process, such as some Web programs, you must use this parameter.

2.API mode

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

Import coverage

CoV = coverage.coverage (Source = [' totest '])

Cov.start ()

#coding

Cov.stop ()

Cov.report ()

Cov.html_report (directory= ' covhtml ')

SOURCE specifies the file to perform statistics, Source = [' totest '] only statistics totest.py coverage

directory specifies the path to generate HTML

Coverage by using coverage statistics Web programs

Web programs typically turn on the service and then loop through the message and do not exit automatically. The coverage is implemented using the Atexit module to register a callback function that writes the in-memory coverage results to the file when the python exits. The test script can only get the coverage result if it exits normally or exits with SIGINT 2 signal to start atexit. Ctrl + C is the SIGINT 2 signal, so the foreground start of the service with CTRL + C to stop after the results .

Using coverage tools to count Python unit test coverage

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.