Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interpret Python coverage.py branch coverage results?

I'm using coverage.py to measure the code coverage of my tests. I've enabled branch coverage, but I can't quite make sense of the report.

Without branch coverage, I get 100% coverage:

Name                           Stmts   Miss  Cover   Missing
------------------------------------------------------------
mylib/queries.py                  44      0   100%

With branch coverage enabled:

Name                           Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------------------
mylib/queries.py                  44      1     20      3    94%   55, 21->10, 53->-48, 59->-58

The source in question can be found here.

21->10 makes sense; the if clause never evaluates to False (jumping back to the beginning of the outer for loop).

However, 53->-48 and 59->-58 have me scratching my head. What do they mean?

like image 322
David Eyk Avatar asked May 18 '16 15:05

David Eyk


People also ask

What does branches mean in code 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.

How does Python coverage work?

Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not. Coverage measurement is typically used to gauge the effectiveness of tests.


1 Answers

Dan's comment is close. First, the negative numbers mean, an exit from a function starting at that line number. So -48 means, exit from the function starting at line 48.

The issue isn't empty loops: those branches would have happened if the loops ever completed. It looks like perhaps they did not.

BTW: Coverage.py 4.1b3 has changed some of this behavior: they'd be marked as 53->exit, 59->exit. Also, the branches themselves might be identified differently. Give it a try.

like image 71
Ned Batchelder Avatar answered Oct 12 '22 12:10

Ned Batchelder