测试插件介绍(maven-plugin-plugin)
Maven所做的只是在构建执行到特定生命周期阶段的时候,通过插件来执行JUnit或者TestNG的测试用例。
这一插件就是maven-surefire-plugin,可以称之为测试运行器(Test Runner)
default生命周期test阶段:使用单元测试框架运行测试
在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java)下所有符一组命名模式的测试类。
- **/Test*.java:
- **/*Test.java:
- **/*TestCase.java:
只要将测试类按上述模式命名,Maven就能自动运行它们,
注意:以Tests结尾的测试类不会得以自动执行
跳过测试
mvn package -DskipTests
也可以在POM配置文件中加参数
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Maven也允许跳过编译,但不推荐这么做
mvn package -Dmaven.test.skip=true
跳过编译的POM配置
<!-- 使用UTF-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>true</skip>
</configuration>
</plugin>
<!-- 配置测试插件参数 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
手动指定要运行的测试用例
只运行account-captcha的RandomGeneratorTest
mvn test -Dtest=RandomGeneratorTest -DfailIfNoTests=false
test参数还支持高级赋值方式
mvn test -Dtest=Random*Test
除星号匹配外,还可以使用逗号指定多个测试用例
mvn test -Dtest=RandomGeneratorTest,AccountCaptchaServiceTest
也可以结合使用星号和逗号
mvn test -Dtest=*Test,AccountCaptchaServiceTest
test参数必须匹配一个或者多个测试类,如果maven-surefire-plugin找不到任何匹配的测试类,就会报错并导致构建失败,如
mvn test -Dtest
加上-DfailIfNoTests=false,告诉maven-surefire-plugin即使没有任何测试也不要报错
mvn test -Dtest -DfailIfNoTests=false
包含与排除测试用例
通过额外的配置来自定义包含一些其他测试类,或者排除一些符合默认命名模式的测试类
自动运行以Tests结尾的测试类
<!-- 自动运行以Tests结尾的测试类 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
排除一些符合默认命名模式的测试类
<!-- 排除运行测试类 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<exclude>**/*ServiceTest.java</exclude>
</excludes>
</configuration>
</plugin>
测试报告
默认情况下,maven-surefire-plugin会在项目的target/surefire-reports目录下生成两种格式的错误报告:
简单文本格式
与JUnit兼容的XML格式
测试覆盖率报告
测试报告覆盖率是衡量项目代码质量的一个重要参考指标
Cobertura是一个优秀的开源测试覆盖率统计工具(http://cobertura.sourceforge.net/),
Maven通过cobertura-maven-plugin与之集成,用户可以使用简单命令为Maven项目生成测试覆盖率报告,如:
mvn cobertura:cobertura
执行完成后,打开target/site/cobertura/下的index.html文件,就能看到测试覆盖率报告
运行TestNG测试
TestNG是Java除JUnit之外另一个流行的单元测试框架。
NG是Next Generation
相关资料:
http://testng.org
《Next Generation Java Testing》(Java测试新技术)
首先要删除POM中的JUnit依赖,加入TestNG依赖,如:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
TestNG允许用户使用一个名为testng.xml的文件来配置想要运行的测试集合
可以在account-captcha的项目根目录下创建一个testng.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" verbose="1">
<test name="Regression1">
<classes>
<class name="com.juvenxu.mvnbook.account.captcha.RandomGeneratorTest"/>
</classes>
</test>
</suite>
配置maven-surefire-plugin使用testng.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
TestNG较JUnit的一大优势在于它支持测试组的概念,如下的注解会将测试方法加入到两个测试组util和medium中
package com.juvenxu.mvnbook.account.captcha;
import static org.junit.Assert.assertFalse;
import java.util.HashSet;
import java.util.Set;
import org.testng.annotations.Test;
public class RandomGeneratorTest {
@Test (groups = { "util", "medium" })
public void testGetRandomString() throws Exception {
Set<String> randoms = new HashSet<String>(100);
for (int i = 0; i < 100; i++) {
String random = RandomGenerator.getRandomString();
assertFalse(randoms.contains(random));
randoms.add(random);
}
}
}
由于用户可以自由地标注方法所属的测试组,因此这种机制能让用户在方法级别对测试进行归类
Maven用户可以使用如下配置运行一个或者多个TestNG测试组
重用测试代码
mvn package,Maven会将项目的主代码及资源文件打包,将其安装或部署到仓库之后,这些代码就能为他人所用,从而实现Maven项目级别的应用。
默认的打包行为不会包含测试代码的,因此在使用外部依赖的时候,其构件一般不会包含测试代码
在项目内部重用某个模块的测试代码是很常见的需求,可能某个底层模块的测试代码中包含了一些常用的测试工具类,或者一些高质量的测试基类供继承。这个时候,Maven用户就需要通过配置maven-jar-plugin将测试类打包,如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
maven-jar-plugin有两个目标,分别是jar和test-jar,前者通过Maven的内置绑定在default生命周期的package阶段运行,其行为就是对项目主代码进行打包,而后者并没有内置绑定,因此上述插件配置显式声明该目标来打包测试代码
上述依赖声明中有一个特殊的元素type,所有测试包构件都使用特殊的test-jar打包类型。需要注意的是,这一类型的依赖同样都使用test依赖范围。
-----------------------------------------------------------------------------------
@author Free Coding http://ln-ydc.iteye.com