PyTest: Calculate code coverage

Personal GitHub site

PyTest: Calculate code coverage

How To

To calculate code coverage in Python, you can use several tools. I chose for a plugin called pytest-cov to keep everything running inside the context of PyTest.

Install pytest-cov

You can install pytest-cov using either pip or poetry using:

Get a coverage report in the terminal

To get a coverage report in the terminal, you can run the following command, assuming you have a poetry shell active:

poetry run pytest --cov

This will result in something like the following table:

Name                        Stmts   Miss  Cover
-----------------------------------------------
hashtable_tdd\__init__.py       1      0   100%
hashtable_tdd\main.py          26      0   100%
tests\__init__.py               0      0   100%
tests\test_hashtable.py        59      1    98%
-----------------------------------------------
TOTAL                          86      1    99%

Generate a HTML coverage report

To generate this report in HTML, including a more detailed per-file inspection page, run the following command in a active poetry shell:

poetry run pytest --cov --cov-report=html

This will create a folder called html-cov in the execution location which will contain the HTML report:

PyTest coverage report index page

PyTest coverage report detail page

References