Sonarqube: view unit tests that cover the source

2019-02-13 21:46发布

We have a CI setup in Bamboo which runs Junit Tests and computes Unit Test Coverage using Jacoco. Then we run Sonar plugin for source code analysis. Everything is working great and we can see the analysis on the SonarCube server, inlcuding coverage, but we would like to see exactly which tests cover certain line of code. Right now it just says: covered by unit tests.

Is there a way to do that?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-13 22:30

I found an answer in the sample sonar project: https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/ut/ut-maven-jacoco.

Jacoco listener has to be configured for surefire plugin.

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- Minimal supported version is 2.4 -->
        <version>2.13</version>
        <configuration>
          <properties>
            <property>
              <name>listener</name>
              <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
          </properties>
        </configuration>
      </plugin>

and dependency added:

   <dependency>
      <groupId>org.sonarsource.java</groupId>
      <artifactId>sonar-jacoco-listeners</artifactId>
      <version>3.8</version>
      <scope>test</scope>
    </dependency>

Also some combination of the following parameters for sonar plugin was needed:

sonar.java.surefire.reportspath 
sonar.junit.reportsPath
sonar.tests=src/test

The last one sealed the deal

查看更多
登录 后发表回答