码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-06-09 10:08:35      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:snap   测试的   模块   代码   info   http   多个   col   使用   

1 JaCoCo介绍

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

2 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文件,即可查看报告。

3 Maven多模块接入

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

3.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>

3.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>

3.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文件并打开即可查看报告。

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

技术图片

 

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

标签:snap   测试的   模块   代码   info   http   多个   col   使用   

原文地址:https://www.cnblogs.com/ycyzharry/p/10992416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!