I am measuring code coverage of a small Python application.
Although line coverage is 100%, branch coverage is not. The problem is that coverage
won't give me any indication about the location of branches not being covered.
coverage run
--branch
--omit=/usr/lib/python3/dist-packages/*,tests/*
-m unittest discover
returns:
Ran 33 tests in 0.079s
OK
Name Stmts Miss Branch BrMiss Cover Missing
-------------------------------------------------------------
app/__init__ 1 0 0 0 100%
app/file_finder 93 0 40 0 100%
app/zipper 66 0 46 7 94%
-------------------------------------------------------------
TOTAL 160 0 86 7 97%
I would expect Missing
column contain the lines corresponding to seven missed branches, but there is nothing there.
How should I find them?
To calculate Branch Coverage, one has to find out the minimum number of paths which will ensure that all the edges are covered. In this case there is no single path which will ensure coverage of all the edges at once. The aim is to cover all possible true/false decisions.
Branch coverage is a metric that indicates whether all branches in a codebase are exercised by tests. A "branch" is one of the possible execution paths the code can take after a decision statement—e.g., an if statement—gets evaluated.
To achieve 100% condition coverage, test cases need to make a > 0 as true and false as well as b > 0 as true and false. However, t1 and t2 fail to make a > 0 as false so that the 100% condition coverage is not satisfied.
The goal of decision coverage testing is to cover and validate all the accessible source code by checking and ensuring that each branch of every possible decision point is executed at least once. In this coverage, expressions can sometimes get complicated. Therefore, it is very hard to achieve 100% coverage.
-m
will only include the "full" misses, not the branch misses. You can use the command
coverage html
to create a set of HTML pages that includes highlighting for all of the coverage, including branch misses. See e.g. this example of HTML reporting, from the documentation.
If you are brave enough to try a pre-release version, coverage.py 4.0 will show missing branches in the textual report also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With