Coverage.py — Coverage.py 5.1 documentation (original) (raw)
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.
The latest version is coverage.py 5.1, released April 12, 2020. It is supported on:
- Python versions 2.7, 3.5, 3.6, 3.7, 3.8, and 3.9 alpha.
- PyPy2 7.3.0 and PyPy3 7.3.0.
For Enterprise¶
Available as part of the Tidelift Subscription.
Coverage and thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. If you want the flexibility of open source and the confidence of commercial-grade software, this is for you. Learn more.
Quick start¶
Getting started is easy:
- Install coverage.py:
For more details, see Installation. - 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:
If you usually use:
then you can run your tests under coverage with:
$ coverage run -m pytest arg1 arg2 arg3
Many people choose to use the pytest-cov plugin, but for most purposes, it is unnecessary.
Change “python” to “coverage run”, so this:
$ python -m unittest discover
becomes:
$ coverage run -m unittest discover
Nose has been unmaintained for a long time. You should seriously consider adopting a different test runner.
Change this:
to:
$ coverage run -m nose arg1 arg2
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:
$ 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%
4. 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 areport like this.
Using coverage.py¶
There are a few different ways to use coverage.py. The simplest is thecommand 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 theAPI.
Some test runners provide coverage integration to make it easy to use coverage.py while running tests. For example, pytest has the pytest-covplugin.
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.pyfor details.
More information¶
- Installation
- For enterprise
- Coverage.py command line usage
- Configuration reference
- Specifying source files
- Excluding code from coverage.py
- Branch coverage measurement
- Measuring sub-processes
- Measurement contexts
- Coverage.py API
- How coverage.py works
- Plug-ins
- Contributing to coverage.py
- Things that cause trouble
- FAQ and other help
- Major changes in 5.0
- Change history for coverage.py
- Sleepy Snake