一、Jacoco 簡(jiǎn)介
Jacoco可以嵌入到Ant、Maven中骇两,也可以使用Java Agent技術(shù)監(jiān)控任意Java程序宴咧,也可以使用Java Api來定制功能朝群。
Jacoco會(huì)監(jiān)控JVM中的調(diào)用燕耿,生成監(jiān)控結(jié)果(默認(rèn)保存在jacoco.exec文件中),然后分析此結(jié)果姜胖,配合源代碼生成覆蓋率報(bào)告缸棵。
二、基于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>
假設(shè)說原本的項(xiàng)目用的是junit 后來改用testng,這時(shí)候不需要修改junit的tastcase隧出,只需配testng.xml ,testng可以執(zhí)行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、執(zhí)行測(cè)試
4胀瞪、測(cè)試結(jié)果
執(zhí)行成功后會(huì)在target/site目錄下生成html覆蓋率報(bào)告