Using Coverage to generate reports (Python)

A couple of tips so I don't have to look it up again. A few months back I wanted to generate coverage data for a stand-alone application, with a few tweaks.

Combining the output of Trial and Python unit tests

$ coverage run -p tests/
$ coverage run -p tests/
$ coverage combine
$ coverage html -d tests/coverage_html  --include=./*py --omit='lib/*.py,tests/*.py'

Helpful resources to get used to combine:

Getting coverage report for a simple project

$ coverage run tests/
$ coverage report --include=./*py --omit='lib/*.py,tests/*.py' # For a text summary
$ coverage html -d tests/reports  --include=./*py --omit='lib/*.py,tests/*.py' # For a nice HTML report

Installing the latest version

To get access to the command line tools, I had to install Coverage in a virtualenv to get around some other constraints on the system. I thought I'd build it from master while I was at it :)

$ virtualenv coverage
$ cd coverage
$ source bin/activate
$ pip install hg+

Coverage is a lovely library and a wonderful way to encourage yourself to think more about your unit tests -- give it a try if you haven't already. (PS: For Django, see django-coverage)

Want to receive a weekly digest of new articles?