Python Coverage
Last updated
Was this helpful?
Last updated
Was this helpful?
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 is easy:
Use coverage run
to run your test suite and gather data. However you normally run your test suite, you can run your test runner under coverage. If your test runner command starts with “python”, just replace the initial “python” with “coverage run”.
Instructions for specific test runners:pytestunittestnosetes
Nose has been unmaintained for a long time. You should seriously consider adopting a different test runner.
Change this:
to:
To limit coverage measurement to code in the current directory, and also find files that weren’t executed at all, add the --source=.
argument to your coverage command line.
Use coverage report
to report on the results:
For a nicer presentation, use coverage html
to get annotated HTML listings detailing missed lines:
Then open htmlcov/index.html in your browser, to see a report like this.
There are a few different ways to use coverage.py. The simplest is the command line, which lets you run your program and see the results. If you need more control over how your project is measured, you can use the API.
Some test runners provide coverage integration to make it easy to use coverage.py while running tests. For example, pytest has the pytest-cov plugin.
You can fine-tune coverage.py’s view of your code by directing it to ignore parts that you know aren’t interesting. See Specifying source files and Excluding code from coverage.py for details.