Skip to content

Code Coverage

Code coverage is an important metric to help you understand how well tested your software is. There are multiple criteria that can be used to determine if your code was tested well. A few examples include: function coverage, branch coverage, and line coverage. It is important to note that, when working on a repository, you try not to decrease the test coverage with your pull request.

Installing pyest-cov Coverage Tool

To measure the coverage of our code, we can use pytest-cov, which is installed by simply running:

Text Only
pip install pytest-cov

Below is an example script with corresponding tests:

Python
class Calculator:
    def add(x, y):
        return x + y

    def subtract(x, y):
        return x - y

    def multiply(x, y):
        return x * y

    def divide(x, y):
        if y == 0:
            return 'Cannot divide by 0'
        return x * 1.0 / y
Python
from ..calculator import Calculator


def test_add():
    assert Calculator.add(1, 2) == 3.0
    assert Calculator.add(1.0, 2.0) == 3.0
    assert Calculator.add(0, 2.0) == 2.0
    assert Calculator.add(2.0, 0) == 2.0
    assert Calculator.add(-4, 2.0) == -2.0

def test_subtract():
    assert Calculator.subtract(1, 2) == -1.0
    assert Calculator.subtract(2, 1) == 1.0
    assert Calculator.subtract(1.0, 2.0) == -1.0
    assert Calculator.subtract(0, 2.0) == -2.0
    assert Calculator.subtract(2.0, 0.0) == 2.0
    assert Calculator.subtract(-4, 2.0) == -6.0

def test_multiply():
    assert Calculator.multiply(1, 2) == 2.0
    assert Calculator.multiply(1.0, 2.0) == 2.0
    assert Calculator.multiply(0, 2.0) == 0.0
    assert Calculator.multiply(2.0, 0.0) == 0.0
    assert Calculator.multiply(-4, 2.0) == -8.0

def test_divide():
    assert Calculator.divide(1, 2) == 0.5
    assert Calculator.divide(1.0, 2.0) == 0.5
    assert Calculator.divide(0, 2.0) == 0
    assert Calculator.divide(-4, 2.0) == -2.0

Copy and paste the above script and tests to files with the corresponding names. To run the tests and see their coverage, use the following:

Text Only
pytest --cov

Result

Text Only
============================= test session starts ==============================
platform darwin -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/name/dir
plugins: cov-3.0.0
collected 2 items

test_calculator.py ....                                                 [100%]

---------- coverage: platform darwin, python 3.9.12-final-0 ----------
Name                           Stmts   Miss  Cover
--------------------------------------------------
calculator.py                     11      1    91%
test_calculator.py                25      0   100%
--------------------------------------------------
TOTAL                             36      1    97%


============================== 2 passed in 0.11s ===============================

Codecov

An additional tool for visualising code coverage and integrating it into your repositories is CodeCov. To familiarise yourself with it, I recommend using their tutorial which can be found here.