欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于SpringBoot单元测试(cobertura生成覆盖率报告)

程序员文章站 2022-06-26 10:20:29
目录demo(springboot 项目)覆盖率测试报告生成(cobertura)cobertura 原理1.instrument2.执行测试3.生成报告springboot pom.xml 配置命令...

demo(springboot 项目)

被测试类:

import org.springframework.stereotype.service;
@service
public class testservice {
    public string sayhi() {
        return "hi";
    }
    public int divide(int a, int b) {
        return a / b;
    }
}

测试代码:

import static org.junit.assert.*;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith(springrunner.class)
@springboottest
public class testservicetest {
    @autowired
    testservice testservice;
    @test
    public void testsayhi() {
        testservice testservice = new testservice();
        string result = testservice.sayhi();
        assertequals("hi", result);
    }
    @test
    public void testdivide() {
        testservice testservice = new testservice();
        int result = testservice.divide(3, 6);
        asserttrue(result > -1);
    }
}

pom.xml 配置文件

![cobertura](c:\users\jiaflu\desktop\cobertura.png)<?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 http://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.1.5.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.jiaflu</groupid>
    <artifactid>learn_springoot</artifactid>
    <version>0.0.1-snapshot</version>
    <name>learn_springoot</name>
    <description>demo project for spring boot</description>
    <properties>
        <java.version>1.8</java.version>
        <jackson.version>2.9.8</jackson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-surefire-plugin</artifactid>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupid>org.codehaus.mojo</groupid>
                <artifactid>cobertura-maven-plugin</artifactid>
                <version>2.5.2</version>
                <configuration>
                    <encoding>utf-8</encoding>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

运行mvn cobertura:cobertura 查看截图:

关于SpringBoot单元测试(cobertura生成覆盖率报告)

覆盖率测试报告生成(cobertura)

cobertura 原理

cobertura执行过程大致如下:

  • 使用instrument修改我们编译后的class文件,位于 target\generated-classes。
  • 执行测试,测试数据输出到xxx.ser中,位于 target\cobertura\cobertura.ser。
  • 使用report生成覆盖率报告。

1.instrument

instrument:cobertura使用instrument修改我们编译后的class文件,在代码里面加入cobertura的统计代码。并生成一个.ser文件(用于覆盖率数据的输出)。

使用 instrument 执行的过程中,coberturainstrumenter 会首先调用分析监听器分析给定的编译好的.class,获得touchpoint(可以认为对应于源代码中的待覆盖行)以及需要的其他信息。然后调用注入监听器将信息注入到新的.class中,保存到 \target\generated-classes 目录下。

示例:

//
// source code recreated from a .class file by intellij idea
// (powered by fernflower decompiler)
//
package com.cisco.webex.cmse.soa.soaservice.service;
import net.sourceforge.cobertura.coveragedata.hasbeeninstrumented;
import net.sourceforge.cobertura.coveragedata.touchcollector;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.propertysource;
import org.springframework.stereotype.service;
@propertysource({"classpath:application.properties"})
@service
public class propertyservice implements hasbeeninstrumented {
    private static final logger logger;
    @value("${cdp.instance.url}")
    private string cdpinstanurl;
    @value("${soa.instance.url}")
    private string soainstanceurl;
    @value("${github.api.token}")
    public string gitapitoken;
    @value("${github.instance.url}")
    private string githubinstance;
    @value("${github.repo.template.owner}")
    private string tplrepoowner;
    @value("${github.repo.consul.owner}")
    private string consulrepoowner;
    @value("${slm.listen.queue.name}")
    private string slmlistenqueue;
    public propertyservice() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        touchcollector.touch("com.cisco.webex.cmse.soa.soaservice.service.propertyservice", 12);
        super();
    }
    public string getcdpinstanurl() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        touchcollector.touch("com.cisco.webex.cmse.soa.soaservice.service.propertyservice", 33);
        return this.cdpinstanurl;
    }
    ...
    public void setslmlistenqueue(string ()v) {
        boolean var2 = false;
        int __cobertura__branch__number__ = true;
        touchcollector.touch("com.cisco.webex.cmse.soa.soaservice.service.propertyservice", 85);
        this.slmlistenqueue = slmlistenqueue;
        touchcollector.touch("com.cisco.webex.cmse.soa.soaservice.service.propertyservice", 86);
    }
    static {
        boolean var0 = false;
        boolean var1 = true;
        touchcollector.touch("com.cisco.webex.cmse.soa.soaservice.service.propertyservice", 13);
        logger = loggerfactory.getlogger(propertyservice.class);
    }
}

2.执行测试

在执行测试用例时,引用 cobertura 修改过的.class,测试信息写入到cobertura.ser档案文件。

3.生成报告

从cobertura.ser获取覆盖率数据,然后结合src中提供的源代码,生成最终的覆盖率报告,放到了target\site\cobertura路径下。若配置了生成 html 格式的报告,可以通过 index.html 查看覆盖率测试报告。

springboot pom.xml 配置

添加如下依赖:

       <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-surefire-plugin</artifactid>
                <version>2.5</version>
                <configuration>
                    <!-- 此参数用于解决一个坑,下面会说明 -->
                    <argline>-noverify</argline>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.codehaus.mojo</groupid>
                <artifactid>cobertura-maven-plugin</artifactid>
                <version>2.5.2</version>
                <configuration>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                </configuration>
            </plugin>

采坑:

在使用 mvn cobertura:cobertura 命令生成测试覆盖率报告时,出现了如下问题(截取部分,报错原因如下):

reason:

expected stackmap frame at this location.

bytecode:

0x0000000: 2ab4 001b 2bb9 002e 0200 c600 2f2a b400

0x0000010: 1b2b b900 2e02 00c0 0030 b600 34c6 001c

解决方法:

本人使用的是 jdk1.8,添加 jvm 参数 -noverify,可以在 pom 文件中添加配置,配置见上述 pom.xml

网上查资料 jdk1.7 添加 jvm 参数 -xx:-usesplitverifier,(1.8没有 -xx:-usesplitverifier 这参数)

命令介绍

  • cobertura:check

根据最新的源码标记(生成的class文件)校验测试用例的覆盖率,如果没有达到要求,则执行失败.

  • cobertura:check-integration-test

这个命令和cobertura:check功能是一样的,区别是二者绑定的maven生命周期不一样.cobertura:check绑定了test, cobertura:check-integration-test绑定了verify.再说的明白些,maven生命周期中有一个是test跑得单元测试,还有一个是integration-test跑的集成测试.而verify前就是integration-test.即cobertura:check-integration-test比cobertura:check涵盖的测试用例更多.

  • cobertura:clean

这个好理解,就是清理掉目录/target/cobertura/中得文件.目前发现里面就一个文件cobertura.ser.

  • cobertura:cobertura

这个插件的关键命令.标记被编译的文件,运行单元测试,生成测试报告.

  • cobertura:cobertura-integration-test

和cobertura:cobertura做了一样的事情,区别是包含了集成测试用例.

  • cobertura:dump-datafile

在命令行输出覆盖率数据.数据依据是生成的class文件.这个命令我没搞懂他的意义何在.在后面一个有趣的实验我们会用这个命令来更好的理解cobertura-maven-plugin.

  • cobertura:help
  • cobertura:instrument

标记被编译的class文件.执行这个命令会在目录/target/generated-classes/cobertura下生成一套class文件.

maven-surefire-plugin 使用说明

maven本身并不是一个单元测试框架,它只是在构建执行到特定生命周期阶段的时候,通过插件来执行junit或者testng的测试用例。这个插件就是maven-surefire-plugin,也可以称为测试运行器(test runner),它能兼容junit 3、junit 4以及testng。

在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:

  • */test.java:任何子目录下所有命名以test开关的java类。
  • */test.java:任何子目录下所有命名以test结尾的java类。
  • */testcase.java:任何子目录下所有命名以testcase结尾的java类。

maven-surefire-plugin 插件应用:

1.跳过测试

跳过测试运行 mvn package -dskiptests

或者通过 pom 提供该属性:

<plugin>  
    <groupid>org.apache.maven.plugins</groupid>  
    <artifactid>maven-surefire-plugin</artifactid>  
    <version>2.5</version>  
    <configuration>  
        <skiptests>true</skiptests>  
    </configuration>  
</plugin>

跳过测试代码的编译 mvn package -dmaven.test.skip=true

或者通过 pom 提供该属性:

<plugin>  
    <groupid>org.apache.maven.plugin</groupid>  
    <artifactid>maven-compiler-plugin</artifactid>  
    <version>2.1</version>  
    <configuration>  
        <skip>true</skip>  
    </configuration>  
</plugin>

2.动态指定要运行的测试用例

mvn test -dtest=randomgeneratortest

也可以使用通配符:

mvn test -dtest=random*test

或者也可以使用“,”号指定多个测试类:

mvn test -dtest=random*test,accountcaptchaservicetest

如果没有指定测试类,那么会报错并导致构建失败:

mvn test -dtest

这时候可以添加 -dfailifnotests=false 参数告诉 maven-surefire-plugin 即使没有任何测试也不要报错:

mvn test -dtest -dfailifnotests=false

3.包含与排除测试用例

<plugin>  
    <groupid>org.apache.maven.plugins</groupid>  
    <artifactid>maven-surefire-plugin</artifactid>  
    <version>2.5</version>  
    <configuration>  
        <includes>  
            <include>**/*tests.java</include>  
        </includes>  
        <excludes>  
            <exclude>**/*servicetest.java</exclude>  
            <exclude>**/tempdaotest.java</exclude>  
        </excludes>  
    </configuration>  
</plugin>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。