Sonar integrates jacoco to show unit test coverage

(1) Sonar-scanner combined with jacoco

Here we still need to use maven to generate the statistical results of jacoco, so it is recommended to use maven directly to check the project

1. Add the following configuration to the pom file of the project to be detected:

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <argLine>${surefireArgLine}</argLine>
                </configuration>
            </plugin>
               <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <!-- 在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数 传给被测程序,用于监控JVM中的调用。 -->
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>


                    <configuration>
                        <destFile>
                            ${project.build.directory}/coverage-reports/jacoco.exec
                        </destFile>
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>


                </execution>


                <!-- 在程序的verify阶段,执行report测试的程序。 文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec. 
                    参数 includes和excludes可用来选定report中过滤的类。 -->
                <execution>
                    <id>default-report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>


                    <configuration>
                        <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
                    </configuration>


                </execution>


            </executions>
        </plugin>

2. Then execute:
The result file of mvn install jacoco is generated in the target directory

3. Then, create a new sonar-project.properities file under the project with the following contents:
sonar.projectKey=your_project_name
sonar.projectName=your_project_name
sonar.projectVersion=1.0
sonar.sources=src/main
sonar.tests=src/test
sonar.java .binaries=target
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.core.codeCoveragePlugin=jacoco

Specify the exec binary file storage path

sonar.jacoco.reportPaths=[your_path]/jacoco.exec

The following attributes can be optionally added or not of course

sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportMissing.force.zero=false

4. Finally, execute sonar-scanner in the project root directory, and you can see the report containing unit test coverage on the sonar homepage.

(2) Use maven to directly add unit test coverage to sonar. The first two steps of
this method are the same as above. The last command to check the code is:
mvn clean install sonar:sonar -Dmaven.test.failure.ignore=true -Dsonar.core.codeCoveragePlugin=jacoco -Dsonar.jacoco.reportPaths=[your_path]/jacoco.exec -Dsonar.dynamicAnalysis=reuseReports

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325711619&siteId=291194637