Subby
Subby

Reputation: 5480

Jacoco 0% Code Coverage

My Code Coverage in Sonar is showing 0% which isn't true as I do have Unit Tests.

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.

I run sonarqube as a gradle task within my Jenkins environment. My SonarQube instance shows Code Smells and everything but for code coverage, it shows 0%.

Update

I do get an index.html created for the Code Coverage but that's all showing 0% too:

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

Update

Still getting 0% but this is what I have so far:

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

    jacoco {
        version "0.7.8.201607051106"
    }
}

Upvotes: 2

Views: 4105

Answers (2)

Subby
Subby

Reputation: 5480

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. 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.

Upvotes: 1

Brandon McKenzie
Brandon McKenzie

Reputation: 1685

Excerpt from SonarQube Documentation:

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.

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. 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.

To add Jacoco to your project, you will need to add the following to 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). 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).

Upvotes: 2

Related Questions