Jacoco和SonarQube的测试覆盖率不一样

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jacoco和SonarQube的测试覆盖率不一样相关的知识,希望对你有一定的参考价值。

参考技术A https://stackoverflow.com/questions/57601376/sonarqube-jacoco-coverage-discrepancies

Jacoco is based on bytecode analysis. The exec file is combined with the class files to get the final code coverage values. The problem in my case was that the bytecode generated by Eclipse compiler for Java (for Jacoco eclipse plug-in) and that produced by Javac (during analysis on sonar runner) were different. Hence, the code coverage values generated by both tools were different.

https://stackoverflow.com/questions/25376512/code-coverage-percentage-values-in-jacoco-eclipse-plug-in-and-sonarqube-are-diff

`
I was able figure out what was wrong with the setup that we were using, by looking through our SonarQube analysis deeper and comparing package by package what was being analyzed.

SonarQube was analyzing the whole repository, and the default report for coverage that was displayed was reflecting using not only the jacoco.xml but also looking at some code from other types.

To see the accurate code coverage, I had to change where we were looking in SonarQube's analysis, to the directory which housed the Java code exclusively thus aligning the coverage percentages between the two services.

Share
`

SonarQube + Jacoco - 无法读取 Koin 模块测试覆盖率

【中文标题】SonarQube + Jacoco - 无法读取 Koin 模块测试覆盖率【英文标题】:SonarQube + Jacoco - Unable to read Koin module test coverage 【发布时间】:2019-05-15 00:52:24 【问题描述】:

我在尝试将 Jacoco 代码覆盖率导入声纳时遇到了一些问题,这发生在所有 Koin 模块中。

声纳版本: 7.5.0.20127

Jacoco 版本: 0.8.2

错误:

       ERROR: Cannot import coverage information for file 'mmaccount/src/main/java/br/com/project/mmaccount/di/AccountModule.kt', coverage data is invalid. Error: 
java.lang.IllegalStateException: Line 84 is out of range in the file mmaccount/src/main/java/br/com/project/mmaccount/di/AccountModule.kt (lines: 83)
    at org.sonar.api.internal.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:93)
    at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:81)

因此,我的模块类的 o 覆盖率为 0%,但如果我查看 HTML 报告,它似乎工作正常:

Jacoco HTML report

这仅发生在我的 Koin 模块中,我不确定问题是 Koin 的工作原理还是 Jacoco 和 Kotlin 之间的一些不兼容

【问题讨论】:

您找到解决此问题的方法了吗? 【参考方案1】:

我遇到了同样的问题,生成报告时似乎是 jacoco 的问题,有关此问题的更多信息:https://github.com/jacoco/jacoco/issues/763

解决方案:升级到已修复该问题的 0.8.3

现在有了新版本的 jacoco,我的声纳覆盖率为零的课程现在有了真正的覆盖率。

【讨论】:

以上是关于Jacoco和SonarQube的测试覆盖率不一样的主要内容,如果未能解决你的问题,请参考以下文章

iOS单元测试-04-覆盖率上传sonarqube

SonarQube 和 JaCoCo (maven) 命名约定

单元测试覆盖在 Sonarqube 中不可见

SonarQube 覆盖缺少 Jacoco 报告涵盖的一些行

使用 Powermock 在 Sonarqube 中配置 jacoco 以进行集成和单元测试报告

带有 jacoco 插件的 SonarQube 覆盖百分比显示为零,但可以看到单元测试的数量