Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which branches are not covered by tests?

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?

like image 434
Arseni Mourzenko Avatar asked Feb 04 '15 16:02

Arseni Mourzenko


People also ask

How is branch coverage calculated in testing?

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.

What are branches in test coverage?

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.

What are the required test cases to obtain 100% branch coverage?

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.

Does branch coverage give 100% test coverage?

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.


2 Answers

-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.

like image 74
jonrsharpe Avatar answered Nov 11 '22 11:11

jonrsharpe


If you are brave enough to try a pre-release version, coverage.py 4.0 will show missing branches in the textual report also.

like image 33
Ned Batchelder Avatar answered Nov 11 '22 11:11

Ned Batchelder