Python test case Coverage Tool coverage tutorial (command line tools) (translation)

Source: Internet
Author: User
Welcome to my blog Brief Introduction

Coverage is a tool for detecting unit test coverage, which is to check that your test cases are covered by all of the code. coverage command line tools

After you successfully install coverage via PIP install coverage, a coverage executable is generated at the same sibling directory as the Python command. The coverage corresponds to different versions of the Python program, generating different versions of executable programs, such as the coverage3 of the Coverage2,python3 platform for the Python2 platform, and the version number of coverage-x.y,x and Y as Python.

The coverage command has 5 parameters that indicate the coverage action: run-runs a Python program and collects the running data report-generate reports HTML-outputs the results in HTML format XML-outputs the results in XML format annotate- Run a python program and collect the running data erase-clear the data that was collected before coverage combine-merge coverage collected data debug-get debug information help-view coverage helpful information, coverage Action/coverage action –help to view help information for the specified action. You can specify the configuration file for the command runtime by –rcfile=file. All command line parameters can be written to the configuration file. Run Command

Use the Coverage Run command Python program and collect information.

Coverage run test.py #效果和执行python test.py effect is similar

You can also use the-m parameter to specify an exportable module running in a Python file, such as

Coverage run-m Test.test   #执行test文件里的test模块

You can specify the directory in which to run the Python file by –source,–include,–omit. But be sure to put these three parameters behind run, in front of the Python file you are running.

Coverage Run--source=project test.py

Coverage can handle multithreaded programs, but if you're using multiprocessing, Greenlet, Eventlet, gevent, then coverage can't handle it by default, but you can use the –concurrency parameter, Specifies the library that the program uses specifically, which you can handle. By default, coverage does not handle the code of the Python parser, such as the standard library OS, SYS, and so on, if you also want to see the data for these system libraries, use the-l parameter. If some of the code should have been counted, but did not, then add the –timid parameter to run again, this is a relatively slow with the algorithm, so under normal circumstances, less. If you have multiple processes or machines that need to run coverage programs, you can use –parallel-mod to separate the statistics for all processes.

During the run of coverage, coverage produces warnings that affect the statistical process. These warnings mainly include: "Trace function changed, measurement is likely wrong:xxx"

If the code changes during the run, the error is reported, and XXX indicates the modified name. "Module XXX has no Python source"

Used a non-existent python file "Module XXX was never imported"

The XXX module in the running Python file does not exist "no data was collected"

The main possibility is that one line of code in the Python file you want to run is not executed to "Module XXX was previously imported, but is not measured." 38762
module XXX has been imported when coverage is running, and his operation will not be monitored by coverage. result file

By default, the result file generated by coverage is. Coverage, and you can modify the suffix name of the file by modifying the environment variable Coverage_file. You can also combine the results of multiple runs into a single file using-a, otherwise the resulting file is the result of the last run. You can use coverage erase to empty the resulting file before running. Merging result files

Coverage can combine multiple results files, first copy multiple results files to the same directory, and then run the Combine option to merge multiple files into a. coverage file.

    Coverage combine

You can also specify a file name or directory

    Coverage combine Data1.dat windows_data_files/

In this case, coverage does not collect files in the current directory, and if you need to collect the results in the current directory, you need to specify them at the command line.
Coverage will only collect. coverage files, and files in the following format will be collected.

    . coverage.machine1.
    coverage.20120807t212300.
    Coverage.last_good_run.ok

You can use the Run–parallel-mode parameter to control whether the parameter independent result file is run each time, if specified, the resulting file name Machine name, process ID, and random number. For example

    . Coverage. neds-macbook-pro.local.88335.316857
    . Coverage. geometer.8044.799674

If you run coverage on different machines and the resulting file cannot be merged, you can use the paths parameter to indicate the difference between them. Specific can be configured by paths. If the resulting file is not readable when merging, coverage will output a warning. Results Report

Provides four styles of output file format. Corresponds to the Html,xml command respectively. Their command line arguments are consistent. If you want to collect the results of some files in a series of files, you can specify the specific file name and module name. The –include–omit parameter can use regular to specify the files to collect. Specifies that the-i–ignore-error contest ignores errors that cannot be found in the file. –fail-under can specify a number, and when the coverage result is less than this number, the coverage command returns an error code of 2, but this parameter is not valid for the annotate command. Introduction to coverage reports

The simplest report is a summary of the reports command output, including the number of rows executed, the number of rows not executed, and the percentage of coverage.

$ coverage
Name                      stmts   Miss  Cover
---------------------------------------------
my_program.py      4    80%
my_module.py      2    86%
my_other_module.py      6    89%---------------------------------------------
total    87%

The-m parameter can display file lines that are not specifically executed.

$ coverage report-m
Name                      stmts   Miss  Cover       Missing
-------------------------------------- -----------------
my_program.py      4    80%   33-35,
my_module.py      2    86%   8,
my_other_module.py      6    89%   17-23
-------------------------------- -----------------------
Total    87%

If you use branch coverage,branch the results will be displayed in branch and Brpart two columns. For example

$ coverage report-m
Name                      stmts   Miss Branch brpart Cover Missing
-------------------------- -------------------------------------------
my_program.py      4      2    80%   33-35, 36->38,
my_module.py      2      3      0    86%   8,
my_other_module.py           56      6      5      1    89%   17-23, 40->45
----------------------------------------------------- ----------------
Total      3    87%

You specify the file to view the results of a particular file.

$ coverage report-m my_program.py my_other_module.py
Name                      stmts Miss Cover
- ------------------------------------------------------
my_program.py      4    80%   33-35,
my_other_module.py      6    89%   17-23
------------------------------------------- ------------
Total    87%

The

–skip-covered parameter can not export a file with coverage 100%.

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.