Jacoco 0% 代码覆盖率
Jacoco 0% Code Coverage
我在 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
时,我 可以 看到成功的单元测试。
我 运行 sonarqube
作为我的 Jenkins 环境中的 gradle task
。我的 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"
}
}
The Java Plugin reuses reports; it does not generate them. So before trying to configure your analysis to import these reports, make sure they are correctly generated and non-empty.
由于您似乎没有使用 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
,所以它不会找不到有关默认设置的报告)。
我使用 this 插件解决了这个问题。我看到的问题是 Jacoco 试图寻找仪器测试 androidTests
而不是单元测试 tests
。我使用的插件确保它 运行 预先进行测试,根据测试创建报告并让 Jacoco 指向这些测试。
我在 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
时,我 可以 看到成功的单元测试。
我 运行 sonarqube
作为我的 Jenkins 环境中的 gradle task
。我的 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"
}
}
The Java Plugin reuses reports; it does not generate them. So before trying to configure your analysis to import these reports, make sure they are correctly generated and non-empty.
由于您似乎没有使用 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
,所以它不会找不到有关默认设置的报告)。
我使用 this 插件解决了这个问题。我看到的问题是 Jacoco 试图寻找仪器测试 androidTests
而不是单元测试 tests
。我使用的插件确保它 运行 预先进行测试,根据测试创建报告并让 Jacoco 指向这些测试。