jacoco分支覆盖范围和Sonar条件覆盖范围有什么区别? [英] What is the difference between jacoco branch coverage and Sonar condition coverage?

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

问题描述

我正在尝试使用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

  • Coverage : 84%
  • Branch Coverage : 71%
  • Missed : 9
  • Complexity : 24
  • Missed : 6
  • Lines : 69
  • Missed : 0
  • Methods : 8
  • Missed : 0
  • Classes : 1

SonarQube显示措施:

SonarQube displays the measures :


  • 条件覆盖率 62,5%

  • 覆盖率 81,7%

  • 线路覆盖率 92 ,8%

  • 支付 69
  • 的行数
  • 总体条件覆盖率 62,5%

  • 总体覆盖率 81,7%

  • 整体线路覆盖率 92,8%

  • 整体未覆盖的分支 15

  • 整体未覆盖的行 5

  • 未发现的分支 15

  • 裸露的行 5

  • Condition coverage 62,5%
  • Coverage 81,7%
  • Line coverage 92,8%
  • Lines to cover 69
  • Overall condition coverage 62,5%
  • Overall coverage 81,7%
  • Overall line coverage 92,8%
  • Overall uncovered branches 15
  • Overall uncovered lines 5
  • Uncovered branches 15
  • Uncovered lines 5

根据声纳度量定义页面,条件覆盖的声纳键是branch_coverage,所以我认为条件和分支覆盖是相同的。

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)

如果你有两个测试用例


  • test(a == 1,b == 2)

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

由于(cond1& amp; amp; amp; amp; amp; amp; amp; ;& 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


  • test(a == 1,b == 1)

两种工具都使用以下方法计算覆盖率每行分支信息。
我对我的一些代码进行测试,覆盖条件(Sonarqube)的数量与Jacoco报告中的分支总数相匹配 - 但我使用了最新版本的jacoco和Sonarqube /声纳的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üdigerzuDohna的初步分析和贡献(@ 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)。

  • Methods valueOf and values of enum types (GitHub #491).
  • Private empty no-argument constructors (GitHub #529).
  • Methods annotated with @lombok.Generated to better integrate with Lombok >= 1.16.14. Initial analysis and contribution by Rüdiger zu Dohna (@t1) (GitHub #513).
  • Methods annotated with @groovy.transform.Generated to better integrate with Groovy >= 2.5.0. Thanks to Andres Almiray (@aalmiray) for adding the annotation to Groovy (GitHub #610).
  • Part of bytecode for synchronized blocks (GitHub #501).
  • Part of bytecode for try-with-resources statements (GitHub #500).
  • Part of bytecode for finally blocks (GitHub #604).
  • Part of bytecode for switch statements on java.lang.String values (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分支覆盖范围和Sonar条件覆盖范围有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆