A brief introduction to coverage.py

Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not.

Coverage measurement is typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not.

Getting started:

1. Install coverage:

  • pip install coverage
  • easy_install coverage

2. Use coverage to run your program and gather data:

$ coverage run my_program.py arg1 arg2
blah blah ..your program's output.. blah blah

3. Generate reports with coverage:

$coverage -rm

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

4. You can also use coverage to generate reports in other presentation oriented formats like HTML:

$coverage html

You can also use coverage.py with Django. You can run your Django tests along with coverage to check which codes in your app have been tested by your tests. With the coverage data, you can write new tests to test the codes which have not been tested so far by your tests. For example:

  • $coverage -e        //This deletes previous coverage data
  • $coverage -x manange.py test foo_app.FooTest.foo_method        //Execute manage.py from coverage to collect coverage data
  • $coverage -rm | grep ‘foo_app’      // to filter the report to show the coverage of foo_app
  • coverage run –include=”*foo_app*” –omit=”*tests* manage.py test foo_app        //This will include *foo_app* pattern and omit *tests* pattern from your coverage report.

You can also write custom Test Runners using the coverage API to measure code coverage in a more controlled manner. You can find more detailed documentation about coverage here.

3 thoughts on “A brief introduction to coverage.py

  1. I use coverage a lot with nosetests. I set up an alias so I can pass in the package to cover and the test:

    alias nosecover=’nosetests –with-coverage –cover-html –cover-erase –cover-package’

    For example:

    nosecover pulp.server.content.manager test/unit/test_content_manager.py

    That creates cover/index.html which I can pull up to check out the results. It will only list things in the given package which helps keep the clutter down when I’m working in a particular area.

Leave a reply to pingou Cancel reply