maven-surefire-plugin总结
Maven通过Maven Surefire Plugin插件执行单元测试。(通过Maven Failsafe Plugin插件执行集成测试)
在pom.xml中配置JUnit,TestNG测试框架的依赖,即可自动识别和运行src/test目录下利用该框架编写的测试用例。surefire也能识别和执行符合一定命名约定的普通类中的测试方法(POJO测试)。
生命周期中test阶段默认绑定的插件目标就是surefire中的test目标,无需额外配置,直接运行mvn test就可以。
基本配置如下,下文中的配置项如无特殊说明,都位于pom文件的<configuration>节点中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
......
配置内容
......
</configuration>
</plugin>
常用通用配置
- 跳过测试阶段
<skipTests>true</skipTests>
或者
mvn install -DskipTests
或者 (Compliler插件也会根据该参数跳过编译测试类)
mvn install -Dmaven.test.skip=true
- 忽略测试失败
Maven在测试阶段出现失败的用例时,默认的行为是停止当前构建,构建过程也会以失败结束。有时候(如测试驱动开发模式)即使测试出现失败用例,仍然希望能继续构建项目。
<testFailureIgnore>true</testFailureIgnore>
或者
mvn test -Dmaven.test.failure.ignore=true
- 包含和排除特定的测试类
surefire默认的查找测试类的模式如下:
- **/Test*.java
- **/*Test.java
- **/*TestCase.java
自定义包含和排除模式,支持ant-style表达式和 正则表达式(%regex[...], 按.class文件匹配而不是.java)
<includes>
<include>Sample.java</include>
<include>%regex[.*[Cat|Dog].*Test.*]</include>
</includes>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
- 运行指定的用例
指定测试类
mvn -Dtest=TestClassName test
mvn -Dtest=TestCi*le test
mvn -Dtest=TestSquare,TestCi*le test
指定单个测试类中的多个方法(Junit4+, TestNG)
mvn -Dtest=TestCircle#mytest test
mvn -Dtest=TestCircle#test* test
mvn -Dtest=TestCircle#testOne+testTwo test #(Surefire2.12.1+, Junit4.x+)
- 并发执行测试
(mvn命令加-T选项,多模块项目的各个模块可以并行构建。)
两个方式:
方法一是使用parallel 参数,在一个进程中执行多个线程。
Junit4.7+可用值有:methods, classes, both(classesAndMethods), suites, suitesAndClasses, suitesAndMethods, classAndMethods, all。Junit Runner必须继承自orig.junit.runners.ParentRunner或为指定@org.junit.runner.RunWith。
线程数配置:useUnlimitedThreads 为true,不限制线程数。useUnlimitedThreads 为false时可以使用threadCount和perCoreThreadCount参数。还可以通过threadCountSuites,threadCountClasses,threadCountMethods在不同粒度限制线程。parallelTestsTimeoutInSeconds和parallelTestsTimeoutForcedInSeconds参数设置线程的超时时间。Junit中@NotThreadSafe注解的内容会单线程执行,避免并发。
方法二是使用forkCount参数,创建多个测试进程。
如果forkCount参数值后加C,表示乘以CPU核数(如forkCount=2.5C)。reuseForks表示一个测试进程执行完了之后是杀掉还是重用来继续执行后续的测试。 默认配置为forkCount=1/reuseForks=true。进程的测试单元是class,逐个class的传递给测试进程。
可以用systemPropertyVariables 传入系统参数(mvn test -D...或配置元素),也可以使用argLine传入JVM选项。argLine或者systemPropertyVariables配置里中也能用${surefire.forkNumber}占位符,代表每个进程自己的fork编号(1...n),用来向每个进程传入独立的资源配置(forkCount=0时,该占位符值为1)。
如果使用-T n同时执行多个mvn模块,每个模块都会有forkCount个进程,${surefire.forkNumber}的值为1..n*forkCount。
surefire2.14之前的版本使用forkMode进行配置,对应关系如下。
| Old Setting | New Setting |
| forkMode=once (default) | forkCount=1 (default), reuseForks=true (default) |
| forkMode=always | forkCount=1 (default), reuseForks=false |
| forkMode=never | forkCount=0 |
| forkMode=perthread, threadCount=N | forkCount=N, (reuseForks=false, if you did not had that one set) |
多种并行方式组合
只要forkCount不为0,就可以和-T组合。
forkCount=0, 或forkCount=1/reuseForks=true,可以和parallel自由组合。
forkCount的测试进程是按类为单位执行的,测试类整个整个的传到测试进程中执行。reuseForks=false或forkCount>1时,就会使用独立的测试进程,所以parallel=classes就失效了。但是还是可以组合parallel=methods/threadCount=n指定每个测试进程里的并发线程数。
其他不常用的配置列在最后。
POJO测试
- 不使用测试框架,直接编写名称为**/*Test类,其中的test*方法也会被surefire执行。
- 类中定义的public void setUp()和public void tearDown()方法也会被surefire识别。
- 验证可使用JAVA assert关键字。
- 无法并发执行。
TestNG
- TestNG默认查找执行test包下的*Test.java。Pom.xml中添加TestNG依赖就能执行testng测试。
- 指定SuiteXML文件
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
- 为TestNG @Parameters 注解提供参数
<systemPropertyVariables>
<propertyName>firefox</propertyName>
</systemPropertyVariables>
- 指定group
<groups>functest,perftest</groups>
- 指定Listeners和Reporters
TestNG支持在测试时附加自定义的listener, reporter, annotation transformer, method interceptor。默认会使用基本的listener生成HTML和XML报告。
Listener实现org.testng.ITestListener接口,会在测试开始、通过、失败等时刻实时发送通知。
Reporter实现org.testng.IReporter接口,在整个测试运行完毕之后才会发送通知,参数为对象列表,包含整个测试的执行结果状况。
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value> <!-- disabling default listeners is optional -->
</property>
<property>
<name>listener</name>
<value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value>
</property>
<property>
<name>reporter</name>
<value>listenReport.Reporter</value>
</property>
</properties>
JUnit
Pom.xml中添加JUnit依赖就能执行Junit测试。
根据引入的Junit依赖版本和是否配置了并发 自动确定使用JUnit 3.8.x, JUnit 4.x (serial provider) 还是 JUnit 4.7(junit-core provider with parallel support),也可以在<plugin>节点里加入<dependencies>元素强行指定执行版本。(详细说明:http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html)
- 指定Listener(JUnit4+)
<properties>
<property>
<name>listener</name>
<value>com.mycompany.MyResultListener,com.mycompany.MyResultListener2</value>
</property>
</properties>
- 指定Categories(Junit4.8+)。分组可以用在测试方法或测试类上。Junit使用接口和类的类型分组,选择注解为@Category(基类)的分组时,所有注解了@Category(子类)的分组也会被选中。
<groups>com.mycompany.SlowTests</groups>
public interface SlowTests{}
public interface SlowerTests extends SlowTests{}
public class AppTest {
@Test
@Category(com.mycompany.SlowTests.class)
public void testSlow() {
System.out.println("slow");
}
@Test
@Category(com.mycompany.SlowerTests.class)
public void testSlower() {
System.out.println("slower");
}
@Test
@Category(com.cmycompany.FastTests.class)
public void testSlow() {
System.out.println("fast");
}
}
- Security Manager
<argLine>-Djava.security.manager -Djava.security.policy=${basedir}/src/test/resources/java.policy</argLine>
Junit3还可以如下配置(forkCount为0时):
<systemPropertyVariables>
<surefire.security.manager>java.lang.SecurityManager</surefire.security.manager>
</systemPropertyVariables>
其他不常用的通用配置
- 失败重跑
mvn -Dsurefire.rerunFailingTestsCount= test #(JUnit需要4.x版本)
重跑的log和报告等详细信息,看这里http://maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html
- 指定VM参数
<argLine>-Djava.endorsed.dirs=...</argLine>
- 系统属性
<systemPropertyVariables>
<propertyName>propertyValue</propertyName>
<buildDirectory>${project.build.directory}</buildDirectory>
[...]
</systemPropertyVariables>
不再推荐使用的方式
<systemProperties>
<property>
<name>buildDir</name>
<value>String类型的值</value>
</property>
</systemProperties>
继承父项目配置的系统属性
<systemProperties combine.children="append">
<property>
[...]
</property>
</systemProperties>
- 配置Classpath
test classpath包括:
- test-classes/
- classes/
- 项目依赖
- 额外配置的classpath元素
要配置classpath,最好是使用添加依赖的方法。如果一定添加额外的ClassPath,这样配置:
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
<additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
</additionalClasspathElements>
移除依赖
<classpathDependencyExcludes>
<classpathDependencyExcludes>org.apache.commons:commons-email</classpathDependencyExcludes>
</classpathDependencyExcludes>
按scope移除依赖
<classpathDependencyScopeExclude>runtime</classpathDependencyScopeExclude>
- compile - system, provided, compile
- runtime - compile, runtime
- test - system, provided, compile, runtime, test
- 调试
默认情况下,surefire在新的进程中执行,命令mvn -Dmaven.surefire.debug test创建的测试进程会等待远程调试器(Eclipse)连接到5005端口。要配置不同的端口命令为:
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test
如果forkCount选项配置为0(mvn -DforkCount=0 test),不会创建新的测试进程,测试在maven主线程中执行。命令mvnDebug -DforkCount=0 test会使maven以调试模式运行,可以将远程调试器连接到maven进程。
- 选择测试框架Provider
可选的有surefire-junit3, surefire-junit4, surefire-junit47和 surefire-testng。指定Provider时,测试框架的依赖配置也不能少。
maven-surefire-plugin的<plugin>节点中添加如下配置。
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
- ClassLoader相关配置
解决classpath长度超过命令行运行的最大参数长度问题。
方法有两个,各有优缺点:
一是使用独立的类加载器,在其中加载classpath内容。
二是使用一个只有META-INF/MANIFEST.MF的jar文件(如booter.jar),在manifest.mf文件中配置classpath。然后以 java -classpath booter.jar MyApp的方式运行。
配置方法
useSystemClassLoader为fasle, 使用独立的类加载器。
useSystemClassLoader为true并且useManifestOnlyJar为true时 ,使用manifest-only JAR
useSystemClassLoader为true并且useManifestOnlyJar为false时,使用最原始的java classpath
执行
mvn -Dsurefire.useSystemClassLoader=false test
或
<useSystemClassLoader>false</useSystemClassLoader>
<useManifestOnlyJar>false</useManifestOnlyJar>
这一部分具体信息可查看:http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html
maven-surefire-plugin总结的更多相关文章
- 学习Maven之Maven Surefire Plugin(JUnit篇)
1.maven-surefire-plugin是个什么鬼? 如果你执行过mvn test或者执行其他maven命令时跑了测试用例,你就已经用过maven-surefire-plugin了.maven- ...
- maven surefire plugin介绍
示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- mavne install 报错org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException
maven install 报错 org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.Invoc ...
- 学习Maven之Maven Enforcer Plugin
1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比 ...
- [Maven] - Eclipse "maven compiler plugin" 冲突解决
刚安装最新的Maven 3.2.5,在eclipse中使用maven的Run As->Maven Install,总会提示: Failed to execute goal org.apache. ...
- [Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes
链接地址:[Selecting Contents for Uber JAR](http://maven.apache.org/plugins/maven-shade-plugin/examples/i ...
- maven jetty plugin
转载:http://blog.163.com/xueling1231989@126/blog/static/1026408072013101311395492/ 前言: 在 maven 下测试调试时, ...
- 施用 maven shade plugin 解决 jar 或类的多版本冲突
施用 maven shade plugin 解决 jar 或类的多版本冲突 使用 maven shade plugin 解决 jar 或类的多版本冲突java 应用经常会碰到的依赖的三方库出现版本 ...
- 解决Maven出现Plugin execution not covered by lifecycle configuration 错误
http://blog.163.com/xh_ding/blog/static/1939032892015222368827/ 解决Maven出现Plugin execution not covere ...
- Jenkins安装maven integration plugin失败解决方法
最近装了一个jenkins准备搞一个自动化测试的持续集成,但是在安装maven integration这个插件时报错,试了几次都是失败! 错误原因如下: javadoc安装失败: java.io.IO ...
随机推荐
- 谈Web前端安全编码
最近开发中涉及到有关输出正确的HTML标签这样的问题,正好对字符编码这块儿多看看,之前对这个方面认识的不深,思考的确实不够,如果下次再碰见类似的问题,若再次不少时间去调研的花,就得不偿失了. 就像正则 ...
- java单例,懒汉&饿汉
* 单例模式Singleton * 应用场合:有些对象只需要一个就足够了,如皇帝 * 作用: 保证整个应用程序中某个实例有且只有一个 * 区别: 饿汉模式的特点是加载类时比较慢,但运行是比较快 ...
- 大白菜U盘启动盘制作工具V5.0如何制作启动系统U盘
1:切换到ISO模式或者直接点击主程序左上角的ISO制作,程序会切换到ISO制作界面. 2:在路径里选好ios文件,点击按钮. 3:打开ISO模式的一键制作启动U盘,点击ISO模式里的按钮,按照图中推 ...
- Linux snmp监控
http://blog.csdn.net/apple_llb/article/details/50494787 http://www.ttlsa.com/monitor/snmp-oid/
- 无需u盘和光盘安装linux
今天折腾linux引导的时候发现一个不用任何移动介质的linux安装方法,即直接在硬盘中启动安装系统. 1.首先下载一个easyBCD.进入“添加新条目”选项选择“NeoGrub”条目,然后选择“添加 ...
- linux socket 编程(C语言)
转自:http://blog.csdn.net/piaojun_pj/article/details/5920888 最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很 ...
- BZOJ 1072: [SCOI2007]排列perm 状态压缩DP
1072: [SCOI2007]排列perm Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为 ...
- ECJTU大一暑假集训
第二场比赛:一签到题没做出来!!!死活不远做下去了,开始发狂,最后还有2个半小时开始做别的,陆续A了几道: 我还能怪谁呢,我渣,我傻逼,就这样!! 7/19:早就想自己建一个博客了,也就是一直想想没 ...
- 电赛总结(二)——AD芯片总结之音频处理芯片ADC8009
一.特性参数 1.专门用来用音频处理的AD芯片 2.内部固定好8K的采样速率 3.8位AD芯片 二.内部结构图 三.芯片管脚图 四.管脚功能说明 管脚名称 功能 IN0~IN7 数据输入端 ABC 数 ...
- BZOJ1103 [POI2007]大都市meg(DFS序)
题目:一颗树,单边修改,链上查询..实际上链是根到结点的链.网上好像有其他做法,我的想法是这样的: 先不看修改,毫无疑问查询只是查询结点的深度:而修改一条边会有什么影响:影响是且只是以边上深度最深结点 ...