我正在尝试使用 SonarQube 扫描仪(版本 3.1.0.1141)分析 java 代码.
I'm trying to analyze java code with the SonarQube Scanner (version 3.1.0.1141).
我已经用这些属性填充了 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 :
SonarQube 显示度量:
SonarQube displays the measures :
根据声纳指标定义页面,条件覆盖的声纳键是分支覆盖率,所以我认为条件和分支覆盖率是一回事.
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
}
你有两个分支
还有两个条件
如果你有两个测试用例
你覆盖了两个分支,因为 (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
这两个工具都使用每行的分支信息来计算覆盖率.我对我的一些代码进行了测试,覆盖条件"(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:
所以我最好的猜测是,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 分支覆盖和声纳条件覆盖有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!