• 使用JaCoCo统计单元测试代码覆盖率


    1 JaCoCo介绍

    JaCoCo是EclEmma团队基于多年覆盖率库使用经验总结而研发的一个开源的Java代码覆盖率库。

    2 JaCoCo覆盖率计数器

    JaCoCo 包含了多种尺度的覆盖率计数器(Coverage Counters),包含指令级(Instructions,C0 coverage)、分支(Branches,C1 coverage)、圈复杂度(Cyclomatic Complexity)、行(Lines)、方法(Methods)、类(Classes)。具体可参考JaCoCo覆盖率计数器

    3 Gradle单模块接入

    在工程的buid.gradle文件中添加如下内容:

    //Applying the JaCoCo plugin
    plugins {
        id 'jacoco'
    }
    
    
    // Configuring JaCoCo plugin settings
    jacoco {
        toolVersion = "0.8.4"
        //reportsDir = file("$buildDir/customJacocoReportDir")
    }
    
    
    //Configuring test task
    jacocoTestReport {
        reports {
            xml.enabled false
            csv.enabled false
            html.enabled true
            //html.destination file("${buildDir}/jacocoHtml")
        }
    }
    
    check.dependsOn jacocoTestReport

    编译完成后会在 ${buildDir}/build/reports/jacoco下生成报告。

    如果在Spring Boot的框架下运行,需要在build.gradle中加下面一段,否则执行jacocoTestReport task会被skipped。

    tasks.withType(Test) {
        scanForTestClasses = false
        include "**/*Test.class" 
    }

    4 Gradle多模块接入

    在父子工程的buid.gradle文件中添加如下内容,注意父和子都要添加:

    //Applying the JaCoCo plugin
    
    apply plugin:'jacoco'
    
    
    // Configuring JaCoCo plugin settings
    jacoco {
        toolVersion = "0.8.4"
        //reportsDir = file("$buildDir/customJacocoReportDir")
    }
    
    
    //Configuring test task
    jacocoTestReport {
        reports {
            xml.enabled false
            csv.enabled false
            html.enabled true
            //html.destination file("${buildDir}/jacocoHtml")
        }
    }
    
    check.dependsOn jacocoTestReport

    编译完成后会在对应的子工程的 ${buildDir}/build/reports/jacoco下生成报告。

    5 Maven单模块接入

    在工程的pom.xml文件中添加如下内容:

    <build>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.4</version>
              <executions>
                 <execution>
                     <id>jacoco-initialize</id>
                     <goals>
                        <goal>prepare-agent</goal>
                      </goals>
                  </execution>
                   <execution>
                     <id>jacoco-site</id>
                     <phase>package</phase>
                     <goals>
                        <goal>report</goal>
                      </goals>
                  </execution>
                </executions>
          </plugin>
        </plugins>
    </build>

    执行Run As Maven build:

    clean install 

    在项目target/site/jacoco目录下找到index.html文件,即可查看报告。

    6 Maven多模块接入

    Maven多模块是指存在父子关联的项目,这类项目中存在多个pom.xml文件,父项目pom.xml文件中包括多个<module>标签,指明它的子模块有哪些,子项目pom.xml中能继承父项目配置的依赖,通过<parent>标签能知道它的父模块是谁。

    6.1 父pom中增加依赖

    在多模块项目中找到父pom.xml文件,添加如下内容:

    <build>
       <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.4</version>
          </plugin>
        </plugins>
       </pluginManagement>
    </build>

    6.2 新建子覆盖率模块

    该模块添加所有要进行单元测试的子工程的依赖,例如,新增一个jacoco-coverage的子模块,在pom.xml文件里添加如下内容:

    增加<dependency>指定统计哪些module

     <dependencies>
                    <dependency>
                         <groupId>org.sonatype.mavenbook.multi</groupId>
                         <artifactId>simple-weather</artifactId>
                         <version>1.0.0-SNAPSHOT</version>
                    </dependency>
                    <dependency>
                         <groupId>org.sonatype.mavenbook.multi</groupId>
                         <artifactId>simple-webapp/artifactId>
                         <version>1.0.0-SNAPSHOT</version>
                    </dependency>
     </dependencies>

    添加jacoco-maven-plugin

    <build>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.4</version>
              <executions>
                 <execution>
                     <id>jacoco-initialize</id>
                     <goals>
                        <goal>prepare-agent</goal>
                      </goals>
                  </execution>
                   <execution>
                     <phase>verify</phase>
                     <goals>
                        <goal>report-aggregate</goal>
                      </goals>
                  </execution>
                </executions>
          </plugin>
        </plugins>
    </build>

    6.3 父pom中增加该模块

    <modules>
      <module>simple-weather</module>
      <module>simple-webapp</module>
      <module>jacoco-coverage</module>
    </modules>

    执行Run As Maven build:

    clean install

    在子项目jacoco-coverage生成的target/site/jacoco-aggregate目录下找到index.html文件并打开即可查看报告。

    以下给出一个官网提供的报告

  • 相关阅读:
    zookeeper curator ( 实战一)
    【转】HDMI之TMDS信号
    【转】LVDS基础、原理、图文讲解
    【原】HDMI输出接口传输速率计算
    【转】 HDMI介绍与流程
    【转】 glibc detected *** corrupted double-linked list:错误的原因有如下三种可能
    【转】 ISP-镜头阴影校正(LSC)
    【转】 ISP-黑电平校正(BLC)
    【转】 ISP概述、工作原理及架构
    【转】 VGA时序及其原理
  • 原文地址:https://www.cnblogs.com/ycyzharry/p/10992416.html
Copyright © 2020-2023  润新知