简体   繁体   English

Jacoco 0%代码覆盖率

[英]Jacoco 0% Code Coverage

My Code Coverage in Sonar is showing 0% which isn't true as I do have Unit Tests. 我在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"
  }
}

When I open up the index.html inside build/reports/tests/release then I can see successful unit tests. 当我在build/reports/tests/release打开index.html可以看到成功的单元测试。

I run sonarqube as a gradle task within my Jenkins environment. 我在Jenkins环境sonarqube作为gradle task运行。 My SonarQube instance shows Code Smells and everything but for code coverage , it shows 0% . 我的SonarQube实例显示Code Smells和所有内容,但code coverage显示0%

Update 更新

I do get an index.html created for the Code Coverage but that's all showing 0% too: 我确实为代码覆盖率创建了一个index.html ,但这也都显示了0%:

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

Update 更新

Still getting 0% but this is what I have so far: 仍然获得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"
    }
}

Excerpt from SonarQube Documentation : SonarQube文档摘录:

The Java Plugin reuses reports; Java插件重用报告; 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. 因此,在尝试配置分析以导入这些报告之前,请确保已正确生成了这些报告且这些报告不是空的。

Since you don't seem to be using the Gradle Jacoco plugin , SonarQube is probably reporting that 0% because you have not generated a report. 由于您似乎未使用Gradle Jacoco插件 ,因此SonarQube可能报告了0%,因为您尚未生成报告。 You will need to add Jacoco to your build and ensure that you have fed SonarQube the path of the generated report ( sonar.jacoco.reportPath ) so it can read it. 您将需要将Jacoco添加到构建中,并确保已将生成的报告的路径( sonar.jacoco.reportPath )送入SonarQube,以便它可以读取它。

To add Jacoco to your project, you will need to add the following to build.gradle : 要将Jacoco添加到您的项目,您需要在build.gradle添加以下build.gradle

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

You will also need to ensure the following: First of all, you need to ensure the jacocoTestReport task is being executed (either by adding it to tasks yourself; or by adding the task to your gradle invocation). 您还需要确保以下各项:首先,您需要确保jacocoTestReport任务正在执行(通过将其自己添加到任务中或将任务添加到gradle调用中)。 Secondly, you need to make sure that SonarQube is looking in the correct location for the test report by setting sonar.jacoco.reportPath to point to your /reports/jacoco directory (it's defaulting to target/jacoco.exec , so it won't find a report on default settings). 其次,您需要通过将sonar.jacoco.reportPath设置为指向/reports/jacoco目录来确保SonarQube在正确的位置查找测试报告(默认为target/jacoco.exec ,因此不会)查找有关默认设置的报告)。

I fixed the issue by using this plugin. 我通过使用插件解决了该问题。 The problem as I see it was that Jacoco was trying to look for Instrumentation Tests androidTests and not Unit Tests tests . 我看到的问题是Jacoco试图寻找Instrumentation Tests androidTests而不是Unit Tests tests The plugin which I used made sure that it ran the tests before hand, created a report based on the tests AND make Jacoco point to those tests. 我使用的插件可以确保事先运行测试,并根据测试创建报告,并让Jacoco指向这些测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM