Jenkins+Jacoco+Sonarqube+Springboot 测试覆盖率分析

1、测试覆盖率的重要性

在做单元测试时,代码覆盖率常常被拿来作为衡量测试好坏的指标,甚至,用代码覆盖率来考核测试任务完成情况,比如,代码覆盖率必须达到80%或 90%。于是乎,测试人员费尽心思设计案例覆盖代码。用代码覆盖率来衡量,有利也有有弊。

2、spring白盒测试

常用的测试手段是debug,Junit测试,也可用postman,pytest,等等测试方式。当然有利也有弊,但是作为java开发工作者,junit测试是必须学会的一种手段。但是测试写了不少还是不知道如何展示结果。下面介绍两种测试结果展示方式 1、sonarqube 2、idea自带展示方式

3、JaCoCo配合maven进行测试

项目中增加部分配置

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>junitDemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>8</java.version>
        <sonar.version>3.6.0.1398</sonar.version>
        <jacoco.version>0.8.5</jacoco.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.66</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco.version}</version>
                    <configuration>
                        <includes>
                            <include>**/service/**</include>
                            <include>**/controller/**</include>
                            <!--<include>**/service/impl/*.class</include>-->
                        </includes>
                        <!-- rules里面指定覆盖规则 -->
                        <rules>
                            <rule implementation="org.jacoco.maven.RuleConfiguration">
                                <element>BUNDLE</element>
                                <limits>  
                                    <!-- 指定方法覆盖到50% -->
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>METHOD</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.50</minimum>
                                    </limit>
                                    <!-- 指定分支覆盖到50% -->
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.50</minimum>
                                    </limit>
                                    <!-- 指定类覆盖到100%,不能遗失任何类 -->
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>CLASS</counter>
                                        <value>MISSEDCOUNT</value>
                                        <maximum>0</maximum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>${sonar.version}</version>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!--这个check:对代码进行检测,控制项目构建成功还是失败-->
                    <execution>
                        <id>check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

配置后利用sonarqube

mvn sonar:sonar -Dsonar.host.url=http://sonar.juneyaoair.com:9000 -Dsonar.login=aaa-Dsonar.password=aaa

后可登录页面查看

在这里插入图片描述
sonarqube页面查看效果
在这里插入图片描述在这里插入图片描述
当然有点尴尬,写了测试用例覆盖率还是0.0%

  1. 这个问题终于在一篇博客里找到了,是因为sonarqube没有安装jacoco这个收集插件!!!!!!!!!
  2. 现在遇到问题,maven聚合项目无法收集每个module下面的测试报告 -----这个问题解决了!解决方式为配置 -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml 这样maven插件回去每个单独的module下收集所有测试报告,但是这个无法在jenkins的sonar-scanner里配置。暂时这个不知道什么原因
  3. Destfile配置后,结果报告为乱码状态无法收集

以上第二个问题找到了解决方案
利用sonar-scanner的modules的方式配置每个具体的modules
在这里插入图片描述

4、利用idea查看当前项目测试用例覆盖情况

右击项目 点击 Run ‘All Tests’ with Coverage

在这里插入图片描述
就可得到以下覆盖率报告

在这里插入图片描述
点击进入后可查看每个方法覆盖情况

在这里插入图片描述
绿色为测试覆盖到的代码,红色是需要覆盖的地方

在这里插入图片描述
同时项目中也会有代码覆盖描述

在这里插入图片描述

点击创建代码测试覆盖率分析

在这里插入图片描述
将这个选项改为JaCoCo 后点击run后会在target下生成当前项目的JaCoCo的测试覆盖率报告html

在这里插入图片描述
将index.html拖到浏览器或者用网页打开

在这里插入图片描述在这里插入图片描述
即可看到测试用例覆盖率报告。

猜你喜欢

转载自blog.csdn.net/qq_35868811/article/details/104626809
今日推荐