PyTest: Test Coverage



examples/pytest/coverage/mymod.py
def fib(n):
    if n < 1:
        raise ValueError(f'Invalid parameter was given {n}')
    a, b = 1, 1
    for _ in range(1, n):
        a, b = b, a+b
    return a

def add(x, y):
    return x + y

def area(x, y):
    if x > 0 and y > 0:
        return x * y
    else:
        return None

examples/pytest/coverage/test_mymod.py
import mymod

def test_add():
    assert mymod.add(2, 3) == 5

def test_area():
    assert mymod.area(2, 3) == 6
    assert mymod.area(-2, 3) == None

def test_fib():
    assert mymod.fib(1) == 1

pip install pytest-cov

pytest --cov=mymod --cov-report html --cov-branch

Open htmlcov/index.html