原文链接: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=perthreadthreadCount=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进程。

  

(九)maven-surefire-plugin常用配置的更多相关文章

  1. 学习Maven之Maven Surefire Plugin(JUnit篇)

    1.maven-surefire-plugin是个什么鬼? 如果你执行过mvn test或者执行其他maven命令时跑了测试用例,你就已经用过maven-surefire-plugin了.maven- ...

  2. maven surefire plugin介绍

    示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  3. 【Android Studio安装部署系列】九、Android Studio常用配置以及快捷键

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 整理Android Studio的常用配置和快捷键. 常用配置 显示行号 临时显示 永久显示 File——Settings——Edi ...

  4. Maven pom文件常用配置,转载

    什么是POM Project Object Model,项目对象模型.通过xml格式保存的pom.xml文件.作用类似ant的build.xml文件,功能更强大.该文件用于管理:源代码.配置文件.开发 ...

  5. maven的一些常用配置

    配置JDK<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...

  6. Maven pom.xml 全配置(二)不常用配置

    Maven pom.xml 全配置(二)不常用配置 这里贴出Maven pom.xml文件中使用率较少的配置参数,如果此篇文档中没有找到你想要的参数,移步Maven pom.xml 全配置(一)常用配 ...

  7. Maven pom.xml 全配置(一)常用配置

    Maven pom.xml 全配置(一)常用配置 这里贴出一个Maven中出现频率较高的配置参数注释,方便理解项目中Maven的配置具体的作用.如果在此博文中没有找到你想看到的参数,可以移步Maven ...

  8. IDEA的常用配置(Maven)一键导入及优化内存

    IDEA的常用配置一键导入 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载如图的压缩包 下载完成后解压缩,点击settings_bak,你会看 ...

  9. maven运行时的配置及命令详解

      上面是指定端口运行程序的,也可以先指定好,直接在上面的地方写jettty:run           当然,如果你是在控制台运行且安装了maven,直接可以进入项目的文件中:mvn jetty:r ...

随机推荐

  1. Null passed to a callee that requires a non-null argument

    OC中定义的方法参数默认是不为空的,如果能够为空需要手动指定__nullable ,我想这个警告是提示开发者警惕可能空参数

  2. MySQL事务锁等待超时 Lock wait timeout exceeded; try restarting transaction

    工作中处理定时任务分发消息时出现的问题,在查找并解决问题的时候,将相关的问题博客收集整理,在此记录下,以便之后再遇到相同的问题,方便查阅. 问题场景 问题出现的场景: 在消息队列处理消息时,同一事务内 ...

  3. PHP 操作结果集对象方法

    <?php header('Content-type:text/html;charset=utf-8'); //建立 或者 关闭mysql服务器 @符号用于屏蔽错误信息 $link=@mysql ...

  4. 0515项目优化和List集合

    0515项目优化和List集合 1. 项目优化 1.1 分析当前情况 问题 数据存储是数组形式,数据类型明确.复用度较低. 需求 Student操作使用的代码,StudentManager想要操作考虑 ...

  5. Spring基础之AOP

    一.AOP能解决什么问题 业务层每个service都要管理事务,在每个service中单独写事务,就会产生很多重复性的代码,而且修改事务时,需要修改源码,不利于维护.为此,把横向重复的代码,纵向抽取形 ...

  6. [JavaWeb基础] 013.Struts2 自定义类型转换器

    很多时候,我们在做web开发的时候经常要用到时间的转换器,Struts2给我们提供了一种类型转换器的接口.下面我们讲讲怎么实现吧. 1.首先我们要定义一个类型转换的类,继承自com.babybus.s ...

  7. 节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter

    节点流(文件流) FileInputStream(字节流)处理视频类的                   FileOutputStream(字节流) FileReader(字符流)处理文本文件    ...

  8. 关于vue的多页面标签功能,对于嵌套router-view缓存的最终无奈解决方法

    最近写我自己的后台开发框架,要弄一个多页面标签功能,之前有试过vue-element-admin的多页面,以为很完美,就按它的思路重新写了一个,但发现还是有问题的. vue-element-admin ...

  9. Rocket - regmapper - RegField

    https://mp.weixin.qq.com/s/7WKB1QxcVzqm2Q7bWcKHzA 简单介绍RegField的实现. 1. 简单介绍 定义寄存器域相关的参数类型. 2. RegFiel ...

  10. Flutter 动画鼻祖之CustomPaint

    老孟导读:CustomPaint可以称之为动画鼻祖,它可以实现任何酷炫的动画和效果.CustomPaint本身没有动画属性,仅仅是绘制属性,一般情况下,CustomPaint会和动画控制配合使用,达到 ...