Test Coverage – Advantage of Instruction Coverage Over Line and Branch Coverage

test-coverage

Our project uses Jacoco for code coverage metrics. Previously, we have checked the line and branch coverage against percentage values. However, the documentation also describes an "Instructions" level, which counts Java byte code instructions. From their documentation:

The smallest unit JaCoCo counts are single Java byte code instructions. Instruction coverage provides information about the amount of code that has been executed or missed. This metric is completely independent from source formatting and always available, even in absence of debug information in the class files.

If we have been using line and branch coverage previously, what is the advantage of switching to instruction coverage? Is it just counting the same things in a different way? Or does it provide more accurate metrics? Conversely, is there a situation where we would want to use line and branch coverage metrics instead of byte code?

Best Answer

There is no advantage of byte-code or line coverage over branch coverage.

If one element (line or instruction) of a branch is covered, then all elements of that branch are covered as well.

If a single line of code contains more than one branch (e.g., in C: (a==0) ? do_b() : do_c()), branch coverage takes it into account.

Moreover, branch coverage is completely independent from source code formatting.

Related Topic