Junit测试覆盖统计工具Jacoco和Cobertura的初试Demo

                                 Jacoco&cobertura使用笔记

准备工作

安装JDK1.8

安装Maven3.6.1

创建java工程如jacoco

在工程jacoco根目录下运行mvn install

 

Jacoco

1. 配置pom.xml文件如下:

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

<groupId>com.test.jacoco</groupId>

<artifactId>testJacoco</artifactId>

<version>1.0.0-SNAPSHOT</version>

<name>JaCoCo</name>

<dependencies>

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>4.11</version>

        <scope>test</scope>

    </dependency>

</dependencies>

<build>

    <plugins>

        <plugin>

            <groupId>org.jacoco</groupId>

            <artifactId>jacoco-maven-plugin</artifactId>

            <version>0.8.4</version>

            <configuration>

                <!--指定生成.exec文件的存放位置-->

                <destFile>target/coverage-reports/jacoco-unit.exec</destFile>

                <!--Jacoco是根据.exec文件生成最终的报告,所以需指定.exec的存放路径-->

                <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>

            </configuration>

            <executions>

                <execution>

                    <id>jacoco-initialize</id>

                    <goals>

                        <goal>prepare-agent</goal>

                    </goals>

                </execution>

                <execution>

                    <id>jacoco-site</id>

                    <phase>test</phase>

                    <goals>

                        <goal>report</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>

</project>
 

2. 在工程jacoco根目录下运行mvn test (其中test是包名),结束后,测试相关结果保存在target目录下,打开target中的index.html查看测试覆盖情况结果:

 

3. 结果展示

 

Cobertura(依旧在上面创建的工程jacoco中实验)

1. 配置pom.xml文件如下:


   <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>
        </plugins>
    </build>

2. 在工程jococo根目录下运行 mvn cobertura:cobertura,运行结束后,测试覆盖统计结果保存在target\site中, 通过index.html查看测试覆盖情况结果

 

3. 结果展示

 

 

 

结果保存在..\jacoco\target\site中,工程demo下载,含测试覆盖统计结果的工程demo下载。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值