Maven + testNG + reportNG + Jacoco
一、maven pom配置
testNG依赖
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
reportNG 依赖
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
com.google.inject 这个也要依赖上,否则会报错
Jacoco 依赖
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase> // 测试期间执行插件
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
maven 插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<!-- 加入编码设置,否则生成的报告会中文乱码 -->
<argLine> @{argLine} -Dfile.encoding=UTF-8</argLine>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
</configuration>
</plugin>
testFailureIgnore 标签设为true, 否则用例有错误,一些插件会不执行
argLine 增加-Dfile.encoding=UTF-8 解决报告打印中文乱码问题,但是要加上 @{argLine},意思是在原有参数上新增,否则jacoco会报错 Skipping JaCoCo execution due to missing execution data file.
二、maven jacoco练习项目
1、创建maven工程
过程忽略
2、配置pom.xml文件
这个是我完整内容
<?xml version="1.0" encoding="UTF-8"?>
<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.glodon</groupId>
<artifactId>jacocoTest</artifactId>
<version>0.1</version>
<name>jacocoTest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<argLine> @{argLine} -Dfile.encoding=UTF-8</argLine>
<suiteXmlFiles>
<suiteXmlFile>testNG.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<enconding>UTF-8</enconding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、编写testNG.xml
完整配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test1">
<classes>
<class name="com.glodon.MyTest"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="com.glodon.MyTest2"/>
</classes>
</test>
</suite>
4、工程目录

5、几个类
1、MyFunction.java
package com.glodon;
public class MyFunction {
private Boolean myBoolean = false;
public void setMyBoolean(Boolean isTrue) {
if (isTrue) {
myBoolean = true;
}
}
public Boolean getMyBoolean() {
return myBoolean;
}
public String isMan(String name) {
String value = "你是男人";
if ("".equals(name)){
value = "你是隐形人";
} else if ("男人".equals(name)) {
value = "你真是是隐形人";
} else {
value = "你不是男人";
}
return value;
}
}
2、MyCount.java
package com.glodon;
public class MyCount {
public int add(int x, int y) {
int m = x;
int n = y;
return m + n;
}
public int sub(int x, int y) {
int m = x;
int n = y;
return m + n;
}
}
3、MyTest.java
package com.glodon;
import org.testng.annotations.Test;
public class MyTest {
MyFunction fun = new MyFunction();
@Test(priority = 1)
public void test1() {
fun.getMyBoolean();
System.out.println("MyTest-----test1");
}
@Test(priority = 2)
public void test2() {
fun.isMan("");
fun.isMan("aa");
System.out.println("MyTest-----test2");
}
}
4、MyTest2.java
package com.glodon;
import org.testng.annotations.Test;
public class MyTest2 {
MyFunction fun = new MyFunction();
@Test
public void test1() {
fun.getMyBoolean();
System.out.println("MyTest2-----test1");
}
@Test
public void test2() {
fun.isMan("");
System.out.println("MyTest2-----test2");
}
}
6、执行
1、到项目根目录下执行:mvn test

2、非工程根目录下制定pom文件
mvn test -f “d:\myEclipseWorkspace\jacocoTest\pom.xml”
7、查看Jacoco报告
目录:target/site/jacoco/index.html



3431



被折叠的 条评论
为什么被折叠?



