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.
.. ifconfig:: not prerelease The latest version is coverage.py 3.7.1, released 13 December 2013. It is supported on Python versions 2.6 through 3.4, and PyPy 2.2.
.. ifconfig:: prerelease The latest version is coverage.py 4.0a5, released 16 February 2015. It is supported on Python versions 2.6, 2.7, 3.3, 3.4, and 3.5a1, as well as PyPy 2.4, and PyPy3 2.4. **This is a pre-release build. The usual warnings about possible bugs apply.** The latest stable version is coverage.py 3.7.1, `described here`_.
Getting started is easy:
Install coverage.py from the coverage page on the Python Package Index, or by using "pip install coverage". For a few more details, see :ref:`install`.
Use
coverage run
to run your program and gather data:$ coverage run my_program.py arg1 arg2 blah blah ..your program's output.. blah blah
Use
coverage report
to report on the results:$ coverage report -m 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%
For a nicer presentation, use
coverage html
to get annotated HTML listings detailing missed lines:$ coverage html
.. ifconfig:: not prerelease Then visit htmlcov/index.html in your browser, to see a `report like this`_.
.. ifconfig:: prerelease Then visit htmlcov/index.html in your browser, to see a `report like this one`_.
There are a few different ways to use coverage.py. The simplest is the :ref:`command line <cmd>`, 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 :ref:`API <api>`.
Some test runners provide coverage integration to make it easy to use coverage while running tests. For example, nose has a cover plug-in.
You can fine-tune coverage's view of your code by directing it to ignore parts that you know aren't interesting. See :ref:`source` and :ref:`excluding` for details.
If the :ref:`FAQ <faq>` doesn't answer your question, you can discuss coverage.py or get help using it on the Testing In Python mailing list.
Bug reports are gladly accepted at the Bitbucket issue tracker. Bitbucket also hosts the code repository. There is a mirrored repo on GitHub.
I can be reached in a number of ways. I'm happy to answer questions about using coverage.py.
.. toctree:: :maxdepth: 1 install cmd config source excluding branch subprocess api plugins contributing trouble faq changes