(九)maven-surefire-plugin常用配置
原文链接:https://www.cnblogs.com/pixy/p/4718176.html
对maven-surefire-plugin有想了解的,看这篇:https://www.cnblogs.com/lvchengda/p/13048191.html
基本配置
如下,下文中的配置项如无特殊说明,都位于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 注解提供参数
<build>
<pluginManagement>
<plugins>
<!-- 添加maven-surefire-plugins插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<!-- 设置参数-->
<systemPropertyVariables>
<phone>123456789</phone>
<pwd>abcd1234</pwd>
<propertyName>${phone}</propertyName>
<propertyName>${pwd}</propertyName>
</systemPropertyVariables>
<suiteXmlFiles>
<!--此处testng.xml即为要运行的testng.xml文件 -->
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
testNG通过@Parameters({ “phone”, “pwd” })获取参数
@Test
@Parameters({ "phone", "pwd" })
public void test(String phone, String pwd) {
System.out.println("phone:"+phone);
System.out.println("pwd:"+pwd);
}
- 指定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
- 指定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=2 test #(JUnit需要4.x版本)
- 指定VM参数
<argLine>-Djava.endorsed.dirs=...</argLine>
- 调试
默认情况下,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进程。
最新文章
- Nexus安装配置
- Yii CModel中rules验证 获取错误信息
- 继承 Bean 配置
- jsonp跨域原理
- 2句代码轻松实现WPF最大化不遮挡任务栏并且具有边框调节效果
- FMDB的简单用法
- django模板语法之include
- 18 UI美化自定义形状shape
- Python——爬虫——爬虫的原理与数据抓取
- Ingenious Lottery Tickets 【排序】
- xenserver挂载新硬盘
- MySQL&#160;性能优化--优化数据库结构之优化数据大小
- js定时器优化
- stm32f103_高级定时器——输入捕获/输出比较中断+pwm=spwm生成
- numpy的常用函数
- HTML 格式化
- element组件知识点总结
- 用Vue-cli生成vue+webpack的项目模板怎么设置为vue1.0版本?
- 第二节:Java开发环境的搭建
- 制作MACOSX10.10.3/10.9安装启动盘U盘的教程