pytest Tutorial – Part 3

Having your tests in place after reading pytest Tutorial – Part 1 and pytest Tutorial – Part 2

you certainly want to know how much of your code is actually tested by your tests.

This is called coverage.

Installation

pip install pytest-cov

Checking the coverage

pytest --cov=<module> test_<module>.py

Example

I wanted to analyze my python package flaskurls for test coverage. this is how you can do it:

pytest --cov=flask_url_mapping tests/
----------- coverage: platform win32, python 3.6.5-final-0 -----------
Name                              Stmts   Miss  Cover
-----------------------------------------------------
flask_url_mapping\__init__.py         1      0   100%
flask_url_mapping\flask_urls.py      73      0   100%
-----------------------------------------------------
TOTAL                                74      0   100%


========================== 10 passed in 0.60 seconds ==========================

HTML Reports

You can generate a nice html report from the test runs with

coverage html