Python Coverage

:: Install

    pip install coverage

Command line usage:

  • run – Run a Python program and collect execution data.
  • report – Report coverage results.
  • html – Produce annotated HTML listings with coverage results.
  • xml – Produce an XML report with coverage results.
  • annotate – Annotate source files with coverage results.
  • erase – Erase previously collected coverage data.
  • combine – Combine together a number of data files.
  • debug – Get diagnostic information.
    $ coverage help
    $ coverage help run

Execution
:: run

    $ coverage run my_program.py

:: report

    $ coverage report -m
    Name                      Stmts   Miss  Cover   Missing
    -------------------------------------------------------
    my_program.py                20      4    80%   33-35, 39
    my_other_module.py           56      6    89%   17-23
    -------------------------------------------------------
    TOTAL                        76     10    87%

: a nicer report

    $ coverage html

## Branch coverage
In addition to the usual statement coverage, coverage.py also supports branch coverage measurement. Where a line in your program could jump to more than one next line, coverage.py tracks which of those destinations are actually visited, and flags lines that haven’t visited all of their possible destinations.

    $ coverage run --branch myprog.py

(source: https://coverage.readthedocs.org)