5

I am running a Gradle build inside the IntelliJ Java IDE. The SonarQube runner Gradle plugin is used along with the JaCoCo Gradle plugin.

Problem: I am getting the message No information about coverage per test., (not a duplicate of this post, see below) and the coverage appears in SonarQube, but only as an overall percentage, not a detailed report per file:

SonarQube Test Coverage View

Am I doing something wrong? Is it a bug in SonarQube maybe (as it was with Cobertura recently)?

Here is my build.gradle:

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'

sourceCompatibility = 1.7
project.version = '1.0'
project.group = 'com.acme.sandbox'
project.description = 'just a test project'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

// JaCoCo test coverage configuration
tasks.withType(Test) { task ->
    jacoco {
        append = false
    }
}

// Sonar configuration
sonarRunner {
  sonarProperties {
    property 'sonar.host.url', 'http://host:port'
    property 'sonar.jdbc.url', 'myJdbcURL'
    property 'sonar.jdbc.username', 'dbuser'
    property 'sonar.jdbc.password', 'dbpass'
    property 'sonar.login', 'builduser'
    property 'sonar.password', 'buildpass'

    property 'sonar.profile', 'profilename'
    property 'sonar.branch', 'DEV'
    property 'sonar.language', 'java'
    property 'sonar.sourceEncoding', 'UTF-8'
    property 'sonar.verbose', 'true'
    //property 'sonar.tests', "$projectDir\\src\\test\\java"
    //property 'sonar.binaries', "${buildDir}\\classes\\main,${buildDir}\\classes\\test"

    //property 'sonar.java.coveragePlugin', 'jacoco'
    property 'sonar.jacoco.reportPath', "${buildDir}\\jacoco\\test.exec"
    property 'sonar.junit.reportsPath', "${buildDir}\\test-results"
  }
}

Now if you execute a clean, followed by sonarRunner, a JaCoCo test.exec file gets created and processed:

<snip>
18:35:27.705 DEBUG - Initializers : 
18:35:27.706 INFO  - Base dir: D:\path\to\JaCoCoTest
18:35:27.706 INFO  - Working dir: D:\path\to\JaCoCoTest\build\sonar
18:35:27.707 INFO  - Source dirs: D:\path\to\JaCoCoTest\src\main\java
18:35:27.708 INFO  - Test dirs: D:\path\to\JaCoCoTest\src\test\java
18:35:27.708 INFO  - Binary dirs: D:\path\to\JaCoCoTest\build\classes\main
<snip>
18:35:27.931 INFO  - JaCoCo IT report not found.
18:35:27.933 INFO  - JaCoCo reports not found.
18:35:27.938 DEBUG - Sensors : JavaSquidSensor -> QProfileSensor -> FindbugsSensor -> CpdSensor -> PmdSensor -> SurefireSensor -> CheckstyleSensor -> InitialOpenIssuesSensor -> ProfileEventsSensor -> ProjectLinksSensor -> VersionEventsSensor -> FileHashSensor -> JaCoCoSensor
<snip>
18:35:34.055 INFO  - Sensor SurefireSensor...
18:35:34.056 INFO  - parsing D:\path\to\JaCoCoTest\build\test-results
18:35:34.116 INFO  - Sensor SurefireSensor done: 61 ms
<snip>
18:35:34.936 INFO  - Sensor JaCoCoSensor...
18:35:34.939 INFO  - Analysing D:\path\to\JaCoCoTest\build\jacoco\test.exec
18:35:35.027 INFO  - No information about coverage per test.
18:35:35.028 INFO  - Sensor JaCoCoSensor done: 92 ms

This post suggests to set sonar.tests, which I've tried to no effect. The log file shows that test sources and binaries are detected to be at the correct locations.

FWIW, I used the same settings with a Maven build and it worked. Can't use Maven here though.

Version numbers:

  • SonarQube 4.3
  • SonarQube Java Ecosystem 2.2.1
  • Gradle 1.12 (also: 1.11)
  • IntelliJ IDEA Ultimate 13.1.3
  • Java 1.7

Here is the full test project for download, and also the full log.

Community
  • 1
  • 1
barfuin
  • 16,865
  • 10
  • 85
  • 132
  • Double backslashes in paths look wrong. With Gradle 1.11, neither `sonar.jacoco.reportPath` nor `sonar.junit.reportsPath` should have to be configured manually (perhaps try with 1.12 just to be sure). – Peter Niederwieser May 26 '14 at 17:34
  • @PeterNiederwieser I tried slashes first, but it makes no difference. If I do not configure `sonar.junit.reportsPath`, JUnit test results are not processed. `sonar.jacoco.reportPath` can be left out, this seems to have no effect. The problem remains unchanged, though. – barfuin May 26 '14 at 17:39
  • The `sonar-runner` plugin already sets both of these properties. Try with 1.12. – Peter Niederwieser May 26 '14 at 17:41
  • Same problem with 1.12. You can leave out both properties with 1.12, but it makes no difference. Maybe I should add the complete little test project to the question ... – barfuin May 27 '14 at 09:51
  • Perhaps try to get help on the Sonar list. They are the experts. From what I know, this typically works fine. – Peter Niederwieser May 27 '14 at 10:46
  • Did you have a look at this example : https://github.com/SonarSource/sonar-examples/blob/master/projects/languages/java/code-coverage/ut/ut-maven-jacoco and especially the profile in the pom : https://github.com/SonarSource/sonar-examples/blob/master/projects/languages/java/code-coverage/ut/ut-maven-jacoco/pom.xml You have to use a listener that will plug into surefire to actually organize the jacoco session by tests and hence get the coveragePerTests information. Please note that this is only to have a breakdown of which lines where covered by which tests. – benzonico May 30 '14 at 06:54
  • Is the code that your are recording coverage for in `src/main/java` or `src/test/java`? The latter may not work out-of-the-box. – Peter Niederwieser May 30 '14 at 07:26
  • @PeterNiederwieser The coverage is supposed be recorded for `src/main/java` only. `src/test/java` contains the tests to run. – barfuin May 30 '14 at 07:40
  • @benzonico Your links refer to Maven, not Gradle, it seems. Are you saying the same principle must be applied for Gradle? – barfuin May 30 '14 at 07:42
  • The same principle can't be applied to Gradle, and I don't think it has to be. – Peter Niederwieser May 30 '14 at 07:43
  • I know this link refers to maven but please note that the "No information about coverage per test" message means that you are note going to be able to break down coverage by unit test. The coverage information by file is normally analyzed by SQ from what I see from the logs you provided. And that is why I am a little bit confused : Is your problem that you don't see the breakdown of coverage by tests in SQ or you don't see coverage on files in SQ ? – benzonico May 30 '14 at 07:47
  • @benzonico I'm not sure I understand you correctly. My problem is that coverage is shown only as a total percentage (see screenshot in the question). I want the same page in SQ to display the coverage per file, including green and red highlighting of source file lines. – barfuin May 30 '14 at 07:56
  • @Thomas I assume that you are analyzing Java files. If it is so, for me the trouble seems to be related to how you generate the JaCoCo report. Can you try to actually visualize it in HTML to be sure of what is in the exec file ? The message "No Information about coverage per test" does not relate to your trouble and is there for another functionality (To be able to see which tests covered a specific line). – benzonico May 30 '14 at 08:29
  • 3
    I don't know who gave -1 for this question, I think it is stupid enough to do so, since this question led me to another interesting investigation about how sonar shows 100% coverage. +1 from me – ZuzEL May 30 '14 at 09:51

1 Answers1

4

This is what is called 'wanted' behaviour. Sonar does not display classes with 100% coverage. In your project you have one class fully covered by tests, so nothing to show.

FYI:

Even if you have 100% covered class, you can look at this class coverage by searching in sonars search box (top-right corner).


If you see .exec and it is of valuable size, then you already collected coverage data.

See my FAQ

I got this code analysis widget for your project:enter image description here

And finally after I added another method uncovered by tests and test coverage dropped to 80%: enter image description here

I got what you need - coverage by line:enter image description here

By the way, your modified build.gradle for my sonar (this // does not work for me):

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'


sourceCompatibility = 1.7
project.version = '1.0'
project.group = 'com.acme.sandbox'
project.description = 'just a test project'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

// JaCoCo test coverage configuration
tasks.withType(Test) { task ->
    jacoco {
        append = false
    }
}

// Sonar configuration
sonarRunner {
    sonarProperties {
        // general information about the SonarQube server
        property 'sonar.host.url', 'http://localhost:9000'
        property 'sonar.jdbc.url', 'jdbc:h2:tcp://localhost:9092/sonar'
        property 'sonar.jdbc.validationQuery', 'select 1'
        property 'sonar.jdbc.driverClassName', 'org.hibernate.dialect.H2Dialect'
        property 'sonar.jdbc.password', 'sonar'
        property 'sonar.jdbc.password', 'sonar'
        property 'sonar.login', 'jenkins'
        property 'sonar.password', 'jenkins'

        // information about this project
        //property 'sonar.profile', 'profilename'
        property 'sonar.branch', 'DEV'
        property 'sonar.language', 'java'
        property 'sonar.sourceEncoding', 'UTF-8'
        property 'sonar.verbose', 'true'
        //property 'sonar.tests', "$projectDir\\src\\test\\java"
        property 'sonar.binaries', "${buildDir}/classes/main/"

        // execute JaCoCo before the SonarQube run to have report file ready
        //property 'sonar.java.coveragePlugin', 'jacoco'
        property 'sonar.jacoco.reportPath', "${buildDir}/jacoco/test.exec"
        property 'sonar.junit.reportsPath', "${buildDir}/test-results"
    }
}
Community
  • 1
  • 1
ZuzEL
  • 12,768
  • 8
  • 47
  • 68
  • It's a Gradle build, and `build/classes/main` is where production class files go (test class files go into `build/classes/test`). – Peter Niederwieser May 30 '14 at 07:33
  • @Thomas sonar.jacoco.itReportPath is for sonars Integration Test data wiget, you don't need it. I will install gradle on my mac, try you project and give you update. – ZuzEL May 30 '14 at 07:55
  • 1
    *So seems that sonar uses not to show line coverage if it is 100%* - That's it! The reason was really that the coverage was at 100%. Once I apply the same settings to a "real" project, everything is fine. Well, they probably didn't test the 100% case. :-) – barfuin May 30 '14 at 15:06
  • The PMD related parsing error is fixed in the latest versions. – barfuin May 30 '14 at 15:06
  • @Thomas sonar.jacoco.reportPath is for Unit tests generated jacoco exec file (for ex: jacocoUT.exec) and sonar.jacoco.itReportPath is for Non-Unit tests (i.e. Integration Tests, acceptance tests, Selenium based etc tests which require separate JVM that Gradle's and these kind of tests require external resources). In my case, I get either a jacocoIT.exec or jacocoAT.exec or jacocoST.exec file for non-Unit tests depending upon what kind of tests I have in my src/xxTest/java or groovy. If you have both .exec or multiple exec files, you can generate combined code coverage reports. – AKS Jul 17 '15 at 02:11