自动化测试单元测试Unit Test Java学习笔记

UnitTest:maven中使用Jacoco计算代码覆盖率

2017-03-25  本文已影响866人  _夏兮

一、Jacoco 简介

jacoco 官网

Jacoco可以嵌入到Ant、Maven中,也可以使用Java Agent技术监控任意Java程序,也可以使用Java Api来定制功能。

Jacoco会监控JVM中的调用,生成监控结果(默认保存在jacoco.exec文件中),然后分析此结果,配合源代码生成覆盖率报告。

二、基于maven的配置jacoco

在pom.xml中添加配置 

<plugin>

                <groupId>org.jacoco</groupId>

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

                <version>0.7.8</version>

                <executions>

                    <execution>

                        <id>default-prepare-agent</id>

                        <goals>

                            <goal>prepare-agent</goal>

                        </goals>

                        <configuration>

                            <destFile>

                                ${project.build.directory}/coverage-reports/jacoco.exec

                            </destFile>

                            <propertyName>surefireArgLine</propertyName>

                        </configuration>

                    </execution>

                    <execution>

                        <id>default-report</id>

                        <phase>test</phase>

                        <goals>

                            <goal>report</goal>

                        </goals>

                        <configuration>

                            <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>

                  <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.16</version>

                <configuration>

                    <argLine>${surefireArgLine} -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m</argLine>

                </configuration>

            </plugin>

<dependency>

            <groupId>org.jacoco</groupId>

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

            <version>0.7.8</version>

        </dependency>

如果是使用junit 使用以上配置。

如果是testng 需要添加 suiteXmlFile。在testng.xml中添加tastclass

<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.16</version>

                <configuration>

                    <suiteXmlFiles>

                        <suiteXmlFile>testng.xml</suiteXmlFile>

                    </suiteXmlFiles>

                    <argLine>${surefireArgLine} -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m</argLine>

                </configuration>

            </plugin>

假设说原本的项目用的是junit 后来改用testng,这时候不需要修改junit的tastcase,只需配testng.xml ,testng可以执行junit的代码。testng.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Converted JUnit suite" >

    <test name="JUnitTests" junit="true">

        <classes>

            <class name="com.yihu.mybatis.dao.TestProjectNameMapper" />

        </classes>

    </test>

</suite>

3、执行测试

mvn clean test

4、测试结果

执行成功后会在target/site目录下生成html覆盖率报告

报告目录 index.html
扫一扫,关注TestDev
上一篇 下一篇

猜你喜欢

热点阅读