Created. Updated to 2.5. Updated to 2.6. Added a problem description for doctest users. Updated to 2.7. Updated to 2.75. Updated to 2.76. Updated to 2.77. Updated to 2.78. Updated to 2.8. Updated to 2.80. Updated to 2.85.

Coverage.py is a Python module that measures code coverage during Python execution. It uses the code analysis tools and tracing hooks provided in the Python standard library to determine which lines are executable, and which have been executed. The original version was written by Gareth Rees. I've updated it to determine executable statements more accurately.

Installation

To install coverage, unpack the tar file, and run "setup.py install", or use "easy_install coverage".

You will have a coverage module for importing, and a coverage command for command line execution.

There is also a set of tests for coverage: test_coverage.py and coverage_coverage.py. These will only be of interest if you want to modify coverage.py.

Command-line usage

The command line interface hasn't changed from the original version. All of the original instructions still hold. Read that document if you haven't used coverage.py before. Here I'll describe the changes I've introduced.

The identification of executable statements is now more accurate. Docstrings are not flagged as missing statements, nor are "global" statements. Complex multi-line if and elif conditions are handled properly.

Statements can now be excluded from consideration. This is useful if you have lines of code that you know will not be executed, and you don't want the coverage report to be filled with their noise. For example, you may have interactive test clauses at the ends of your modules that your test suite will not execute:

This suite of code can be excluded from the coverage report by adding a specially-formed comment:

The #pragma line can be placed on any line of code. If the line contains the colon that introduces a suite of statements, the entire suite is excluded. Annotated files (created with "coverage -a") will prefix excluded lines with "-".

Programmatic usage

Again, the original instructions still hold, but I've made a few additions.

The form of the exclusion marker can be changed using the exclude() method. It takes a regular expression, and excludes any line that contains a match:

As the example shows, the marker pattern doesn't have to be a comment. Any number of regular expressions can be added by calling exclude a number of times.

The use of a file to hold coverage data can be suppressed by calling use_cache(0) before calling start().

The analysis(module) function still returns a 4-tuple (filename, statement list, missing list, missing string). A new analysis2(module) function extends the return to a 5-tuple (filename, statement list, excluded list, missing list, missing string).

The annotate(modules) function is available to annotate a list of modules, and the annotate_file(filename, statements, excluded, missing) function provides the bulk of annotation in a directly callable form.

Known Problems

Older versions of doctest interfere with coverage's tracing of statements, and you may get reports that none of your code is executing. Use this patch to doctest.py if you are experiencing problems.

History

Changes I've made over time:

Version 2.85, September 14 2008

Version 2.80, May 25 2008

Version 2.78, September 30 2007

Version 2.77, July 29 2007

Version 2.76, July 23 2007

Version 2.75, July 22 2007

Version 2.7, July 21 2007

Version 2.6, August 22 2006

Version 2.5, December 4 2005

Version 2.2, December 31 2004

Version 2.1, December 14 2004

Version 2, December 12 2004

Problems

Coverage.py has been tested successfully on Pythons 2.2.3, 2.3.5, 2.4.3, 2.5.1 and 2.6a3. If you have code that it doesn't handle properly, send it to me! Be sure to mention the version of Python you are using.

See also