(九)maven-surefire-plugin常用配置

摘要:
 JunitRunner必须继承自orig.junit.runners.ParentRunner或为指定@org.junit.runner.RunWith。useUnlimitedThreads为false时可以使用threadCount和perCoreThreadCount参数。还可以通过threadCountSuites,threadCountClasses,threadCountMethods在不同粒度限制线程。parallelTestsTimeoutInSeconds和parallelTestsTimeoutForcedInSeconds参数设置线程的超时时间。argLine或者systemPropertyVariables配置里中也能用${surefire.forkNumber}占位符,代表每个进程自己的fork编号,用来向每个进程传入独立的资源配置。如果使用-Tn同时执行多个mvn模块,每个模块都会有forkCount个进程,${surefire.forkNumber}的值为1..n*forkCount。  surefire2.14之前的版本使用forkMode进行配置,对应关系如下。forkCount=0,或forkCount=1/reuseForks=true,可以和parallel自由组合。

原文链接: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 SettingNew Setting
forkMode=once(default)forkCount=1(default),reuseForks=true(default)
forkMode=alwaysforkCount=1(default),reuseForks=false
forkMode=neverforkCount=0
forkMode=perthread,threadCount=NforkCount=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常用配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇谈lispSublime Text 2 入门与总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Hibernate三种状态:瞬时状态、持久化状态、离线状态

三种状态及相互转化 瞬时状态(transient):刚new出来一个对象,还没有被保存到数据库中 持久化状态(persistent):已经被保存到数据库中或刚从数据库中取得 离线状态(detached):数据库中有,但是session中不存在该对象 方法的代码实例(下面代码1-8是连贯的,但由于需要分条说明,因此拆开) (1) save():把一个对象从...

Maven测试篇

 Maven的生命周期:   讲解Maven测试篇之前将首先介绍一下Maven生命周期的相关概念,如果你熟知这部分概念可以略过此小节内容。   通常,我们在构建一个项目的时候,不外乎是对其进行清理、编译、测试和部署等操作。对于大多数项目,我们每次都要重复这些必要的过程,而Maven正是对这些必要的构建过程进行了抽象,它以项目的清理、初始化、编译、测试、...

httprunner学习-hook 机制实现setup和teardown

前言 unittest框架里面有个非常好的概念:前置( setUp )和后置( tearDown )处理器,真正会用的人不多。HttpRunner 实际上也是从用的unittest框架,里面也有前置 setup_hooks 和后置 teardown_hooks 的概念。 setup_hooks: 在整个用例开始执行前触发 hook 函数,主要用于准备工作...

Pytest自动化测试

Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。 1、严重性 如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个类。 它以 allure.severity_level 枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),...

Spring Boot 2.2.x Junit4 升级为Junit5 后的变化、对比 找不到 org.junit.jupiter.api.Test

遇到的问题:使用 maven 创建了一个 parent 项目 A,其 pom.xml 继承 parent 为 spring-boot-starter-parent 2.1.10。 然后创建 module 项目 B,使用 spring initializr 构建项目,用的是 IDEA,当时没有选 Spring Boot 版本,结果默认使用的是 2.2.1。...

ORACLE中能否找到未提交事务的SQL语句

  在Oracle数据库中,我们能否找到未提交事务(uncommit transactin)的SQL语句或其他相关信息呢?  关于这个问题,我们先来看看实验测试吧。实践出真知。   首先,我们在会话1(SID=63)中构造一个未提交的事务,如下所:   SQL> create table test   2  as   3  select * fro...