What Is The Difference Between Jacoco Branch Coverage And Sonar Condition Coverage?

Scotty Moe

Updated on:

This article aims to explore the differences between Jacoco branch coverage and Sonar condition coverage, two metrics used to measure code coverage in software testing.

While both metrics assess the extent of code testing, they employ different approaches and yield varying results.

The background information suggests that Jacoco and SonarQube, the tools used for coverage analysis, calculate coverage based on branch information per line. However, there are discrepancies in the percentage and absolute values obtained.

These differences can be attributed to various factors, such as the JDK version used and the presence of compiler-generated artifacts. For instance, the analysis indicates that Jacoco version 0.8.0 filters out compiler-generated artifacts, potentially reducing the total branches. Sonar-Java, utilizing Jacoco version 0.7.9, does not apply such filtering.

Moreover, the background highlights the need for additional tests to achieve full condition coverage.

In conclusion, understanding the disparities between Jacoco branch coverage and Sonar condition coverage is essential for accurately assessing code testing effectiveness.

What is it?

Jacoco branch coverage refers to the percentage of branches that have been executed during testing. It focuses on the execution of different paths within the code, ensuring that all possible outcomes of branching statements have been tested.

Sonar condition coverage, on the other hand, measures the percentage of conditions (such as boolean expressions) that have been covered by tests. It specifically looks at the coverage of individual conditions within those branches and measures whether all possible combinations of conditions have been tested.

In other words, branch coverage ensures that all paths have been executed, while condition coverage ensures that all conditions within those paths have been tested.

Both metrics provide valuable insights into the thoroughness of testing, but they focus on different aspects of code coverage.

Comparison of Results

In the comparison of results, it is observed that the reported metrics for branch coverage and condition coverage show variations. Both Jacoco and Sonar calculate coverage using branch information per line, but the numbers differ in terms of percentage and absolute values.

For example, in the given scenario, Sonar reports a condition coverage of 62% with Jacoco 0.7.9, while Jacoco 0.8.0 shows a higher coverage percentage. Additionally, there is a difference in the number of uncovered branches, with 13 branches reported by Jacoco and 15 branches reported by Sonar.

This difference in results may be influenced by the JDK version used and the versions of the coverage tools. Upgrading the Sonar-Java plugin or downgrading Jacoco may help align the reported metrics.

Factors Affecting Analysis

Factors that may affect the analysis of coverage metrics include:

  • The JDK version used
  • The versions of the coverage tools employed

In the case of Jacoco branch coverage and Sonar condition coverage, the JDK version used was jdk1.8.0_144.

It is worth noting that the Sonar Java plugin version 4.12.0.11033 uses Jacoco version 0.7.9, while the Jacoco version used in this project was 0.8.0.

This difference in Jacoco versions may have resulted in reduced total branches and discrepancies in the coverage metrics. Additionally, it was observed that Jacoco 0.8.0 filters out compiler-generated artifacts, which could further impact the analysis.

To resolve the differences in coverage results, it is suggested to either upgrade the Sonar-Java plugin or downgrade Jacoco to version 0.7.9.

Leave a Comment