jacoco 分支覆盖和声纳条件覆盖有什么区别?

What is the difference between jacoco branch coverage and Sonar condition coverage?(jacoco 分支覆盖和声纳条件覆盖有什么区别?)

本文介绍了jacoco 分支覆盖和声纳条件覆盖有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SonarQube 扫描仪(版本 3.1.0.1141)分析 java 代码.

I'm trying to analyze java code with the SonarQube Scanner (version 3.1.0.1141).

  • SonarQube 版本:5.6.6
  • Sonar Java 插件版本:4.12.0.11033
  • jacoco 版本:0.8.0

我已经用这些属性填充了 sonar-project.properties:

I have filled the sonar-project.properties with those properties :

# Sonar sources and metadata
sonar.language=java
sonar.sources=src/main
sonar.java.source=1.8
sonar.sourceEncoding=UTF-8
sonar.java.binaries=target/classes
sonar.java.libraries=target/lib

sonar.tests=src/test
sonar.java.coveragePlugin=jacoco
sonar.junit.reportsPath=target/surefire-reports
sonar.surefire.reportsPath=target/surefire-reports

虽然 jacoco 报告给了我一个班级的这个结果:

While jacoco report gives me this result for one class :

  • 覆盖率:84%
  • 分支机构覆盖率:71%
  • 错过:9
  • 复杂度:24
  • 错过:6
  • 行数:69
  • 错过:0
  • 方法:8
  • 错过:0
  • 类:1

SonarQube 显示度量:

SonarQube displays the measures :

  • 条件覆盖率62.5%
  • 覆盖率81.7%
  • 线路覆盖率92.8%
  • 覆盖69
  • 的行
  • 整体状况覆盖率62.5%
  • 整体覆盖率81.7%
  • 整体线路覆盖率92.8%
  • 未发现的分支总数15
  • 未覆盖的总线 5
  • 未覆盖的分支15
  • 未覆盖的线 5

根据声纳指标定义页面,条件覆盖的声纳键是分支覆盖率,所以我认为条件和分支覆盖率是一回事.

According to the sonar metric definition page, the sonar key for condition coverage is branch_coverage, so I thought condition and branch coverage are the same things.

如何解释不同的结果?

推荐答案

假设你有一些构造

if(a == 1 && b == 2) {
  //do this
} else {
  //do that
}

你有两个分支

  • 这样做
  • 这样做

还有两个条件

  • a == 1 (cond1)
  • b == 2 (cond2)

如果你有两个测试用例

  • 测试(a == 1,b == 2)
  • 测试(a == 2,b == 2)

你覆盖了两个分支,因为 (cond1 && cond2) 的组合条件要么为假要么为真,

You're covering both branches because the combined condition of (cond1 && cond2) is either false or true,

但是你只完全覆盖了 cond1 并且只覆盖了 cond2 的一半,即 75% 的条件覆盖率.

But you only cover cond1 fully and only half of cond2, thats 75% condition coverage.

要获得完整的条件覆盖,您需要额外的测试

To get full condition coverage, you need an additional test

  • 测试(a == 1,b == 1)

这两个工具都使用每行的分支信息来计算覆盖率.我对我的一些代码进行了测试,覆盖条件"(Sonarqube)的数量与 Jacoco 报告中的总分支"数量相匹配 - 但我使用了 jacoco 和 Sonarqube/sonar-java 的最新版本.所以除了名称之外,措施是/应该是相同的.

Both tools calculate the coverage using the branch information per line. I run a test on some of my code, and the number of "conditions to cover" (Sonarqube) matches the number of total "Branches" in Jacoco report - but I used the most recent versions for jacoco and Sonarqube/sonar-java. So apart from the name, but measures are/should be the same.

但鉴于您提供的数字,您的分析总体上似乎有些奇怪.不仅百分比值不同,而且绝对值也不同(Jacoco 中的 9 个未发现分支与 Sonarqube 中的 15 个未发现分支).

But given the numbers you provide, there seems something odd with your analysis in general. It's not only the percentage-values that differ, but the absolute ones as well (9 uncovered branches in Jacoco vs 15 uncovered Branches in Sonarqube).

所以我检查了您正在使用的版本 - jacoco 0.8.0 和使用 jacoco 0.7.9 的 sonar-java 插件 v4.11.0.11033.

So I checked the versions you're using - jacoco 0.8.0 and the sonar-java plugin v4.11.0.11033 which uses jacoco 0.7.9.

Jacoco 0.8.0 的发行说明阅读

在创建报告期间,各种编译器生成的工件被过滤掉,否则需要不必要的,有时甚至是不可能的技巧来避免部分或遗漏的覆盖:

During creation of reports various compiler generated artifacts are filtered out, which otherwise require unnecessary and sometimes impossible tricks to not have partial or missed coverage:

  • 方法 valueOf 和枚举类型的值 (GitHub #491).
  • 私有的空无参数构造函数 (GitHub #529).
  • 使用 @lombok.Generated 注释的方法可以更好地与 Lombok >= 1.16.14 集成.Rüdiger zu Dohna (@t1) (GitHub #513) 的初步分析和贡献.
  • 使用 @groovy.transform.Generated 注释的方法可以更好地与 Groovy >= 2.5.0 集成.感谢 Andres Almiray (@aalmiray) 将注释添加到 Groovy (GitHub #610).
  • 同步块的部分字节码 (GitHub #501).
  • try-with-resources 语句的部分字节码 (GitHub #500).
  • finally 块的部分字节码 (GitHub #604).
  • java.lang.String 值上 switch 语句的部分字节码 (GitHub > #596).

所以我最好的猜测是,Jacoco 0.8.0 生成的报告过滤掉了一些提到的生成的工件,有效地减少了分支的总数.然而,Sonar-Java 使用 Jacoco 0.7.9,它不会过滤掉生成的工件,因此数量更高(覆盖率更低).

So my best guess here would be, that the report generated by Jacoco 0.8.0 filtered out some of the mentioned generated artifacts effectively reducing to total number of branches. Sonar-Java however uses Jacoco 0.7.9 which does not filter out generated artifacts and therefore the numbers are higher (and the coverage lower).

也许您应该将您的 jacoco 版本降级到 0.7.9 或升级 sonar-java 插件.

Maybe you should either downgrade your jacoco version to 0.7.9 or upgrade the sonar-java plugin.

这篇关于jacoco 分支覆盖和声纳条件覆盖有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:jacoco 分支覆盖和声纳条件覆盖有什么区别?

基础教程推荐