It appears we are only checking line coverage, which is known to overestimate actual code coverage. Although checking path coverage would be preferable, it seems the coverage package does not support it. As such I propose to start focusing on branch coverage.
To put it into context:
def fun(a: bool, b: bool, input: float) -> float:
if a:
print("a is true")
input += 2.0
else:
print("a is false")
input -= 2.0
if b:
print("b is true")
input *= 2.0
else:
print("b is false")
input /= 2.0
return input
fun(False, False, 10.0)
This code snippet would report:
- 9 out of 13 lines covered -> ~69% line coverage
- 2 out of 4 branches covered -> 50% branch coverage
- 1 out of 4 paths covered -> 25% path coverage
Admittedly path coverage is very hard to satisfy, but we shall aim to at least satisfy branch coverage on a per function basis. This boils down to enabling the --branch option of the coverage package.
Kudos to Jason Turner for the nice example.
It appears we are only checking line coverage, which is known to overestimate actual code coverage. Although checking path coverage would be preferable, it seems the
coveragepackage does not support it. As such I propose to start focusing on branch coverage.To put it into context:
This code snippet would report:
Admittedly path coverage is very hard to satisfy, but we shall aim to at least satisfy branch coverage on a per function basis. This boils down to enabling the
--branchoption of thecoveragepackage.