【问题标题】:Jacoco 0% Code CoverageJacoco 0% 代码覆盖率
【发布时间】:2016-11-11 08:32:28
【问题描述】:

我在 Sonar 中的代码覆盖率显示为 0%,这是不正确的,因为我有单元测试。

Gradle

sonarqube {
  properties {
    property "sonar.binaries", "build/intermediates/classes/release"
    property "sonar.java.binaries", "build/intermediates/classes/release"
    property "sonar.java.test.binaries", "build/intermediates/classes/test/release"
    property "sonar.sources", "src"
    property "sonar.junit.reportsPath", "build/reports/tests/release"
    property "sonar.java.junit.reportsPath", "build/reports/tests/release"
    property "sonar.android.lint.report", "build/outputs/lint-results.xml"
    property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/testReleaseUnitTest.exec"
  }
}

当我打开build/reports/tests/release 中的index.html 时,我可以看到成功的单元测试。

我在我的 Jenkins 环境中以 gradle task 的形式运行 sonarqube。我的 SonarQube 实例显示 Code Smells 和除 code coverage 之外的所有内容,它显示 0%

更新

我确实得到了一个为代码覆盖率创建的index.html,但这也都显示为 0%:

app/build/reports/jacoco/jacocoTestDebugUnitTestReport/html/index.html

更新

仍然是 0%,但这是我目前所拥有的:

android {
    ...
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testCoverageEnabled true
        }
        debug {
            testCoverageEnabled true
        }
    }

    jacoco {
        version "0.7.8.201607051106"
    }
}

【问题讨论】:

    标签: jenkins gradle sonarqube jacoco sonarqube-scan


    【解决方案1】:

    摘自SonarQube Documentation

    Java 插件重用报告;它不会生成它们。因此,在尝试配置分析以导入这些报告之前,请确保它们已正确生成且非空。

    由于您似乎没有使用Gradle Jacoco plugin,SonarQube 可能会报告 0%,因为您尚未生成报告。您需要将 Jacoco 添加到您的构建中,并确保您已将生成的报告 (sonar.jacoco.reportPath) 的路径提供给 SonarQube,以便它可以读取它。

    要将 Jacoco 添加到您的项目中,您需要将以下内容添加到 build.gradle

    //...
    apply plugin: "jacoco"
    //...
    jacoco {
        toolVersion = "0.7.6.201602180812"
        //Note: unless "reportsDir" is set here, default is “$buildDir/reports/jacoco”
    }
    

    您还需要确保以下几点:首先,您需要确保正在执行 jacocoTestReport 任务(通过自己将其添加到任务中;或者通过将任务添加到您的 gradle 调用中)。其次,您需要通过将sonar.jacoco.reportPath 设置为指向您的/reports/jacoco 目录(默认为target/jacoco.exec,因此它不会找到默认的报告)来确保SonarQube 正在寻找测试报告的正确位置设置)。

    【讨论】:

    【解决方案2】:

    我通过使用this 插件解决了这个问题。我看到的问题是 Jacoco 试图寻找 Instrumentation Tests androidTests 而不是 Unit Tests tests。我使用的插件确保它事先运行测试,根据测试创建报告并让 Jacoco 指向这些测试。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 2019-01-02
      • 2012-11-02
      • 2014-11-09
      • 1970-01-01
      • 2018-05-28
      • 2012-06-11
      • 1970-01-01
      相关资源
      最近更新 更多