您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   Code  
会员   
 
   
 
 
     
   
 订阅
  捐助
Maven中测试插件(surefire)的相关配置
 
作者 wanghantong博客,火龙果软件    发布于 2014-11-24
  4766  次浏览      17
 

1. 在Maven中配置测试插件surefire

<plugin>  
02. <groupId>org.apache.maven.plugins</groupId>
03. <artifactId>maven-surefire-plugin</artifactId>
04. <version>2.17</version>
05.</plugin>

2. 默认被执行的测试

默认情况下,surefire会执行文件名以Test开头或结尾的测试用例,或者是以TestCase结尾的测试用例。

"**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".  
02."**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
03."**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".</span>

3. 跳过测试 Skipping Tests

<build>  
02. <plugins>
03. <plugin>
04. <groupId>org.apache.maven.plugins</groupId>
05. <artifactId>maven-surefire-plugin</artifactId>
06. <version>2.17</version>
07. <span style="color:#009900;"> <configuration>
08. <skipTests>true</skipTests>
09. </configuration></span>
10. </plugin>
11. </plugins>
12. </build>
13.
14.补充:如果使用Junit或者TestNG也可以直接在当前测试方法上加注解@Ignore忽略,这是加了该注解的也都会被skip

4. 排除测试 Exclusions (Junit & TestNG 通用)

<build>  
02. <plugins>
03. <plugin>
04. <groupId>org.apache.maven.plugins</groupId>
05. <artifactId>maven-surefire-plugin</artifactId>
06. <version>2.17</version>
07. <configuration>
08. <span style="color:#009900;"> <excludes>
09. <exclude>**/TestCircle.java</exclude>
10. <exclude>**/TestSquare.java</exclude>
11. </excludes></span>
12. </configuration>
13. </plugin>
14. </plugins>
15. </build>

5. 仅执行一个/一类测试(repeat) Running a Single Test (Junit & TestNG 通用)

在开发过程中配置命令:

mvn -Dtest=TestCircle test   test表示当前测试方法所在的测试类,
不需要扩展名 The value for the test parameter is the name of the test class
mvn -Dtest=TestCi*le test *表示任何
mvn -Dtest=TestSquare,TestCi*le test 如果测试类没有使用规范的命名,可以显示的直接指定测试方法的名称

Running a set of methods in a Single Test Class  
02.
03.With version 2.7.3, you can run only n tests in a single Test Class.
04. NOTE : it's supported for junit 4.x and TestNG.
05.You must use the following syntax
06.mvn -Dtest=TestCircle#mytest test
07.You can use patterns too
08.mvn -Dtest=TestCircle#test* test
09.As of surefire 2.12.1, you can select multiple methods (JUnit4X only at this time, patches welcome)
10.mvn -Dtest=TestCircle#testOne+testTwo test

6. 如何使用TestNG

<dependency>  
02. <groupId>org.testng</groupId>
03. <artifactId>testng</artifactId>
04. <version>6.3.1</version>
05. <scope>test</scope>
06. </dependency>

If  using an older version of TestNG (<= 5.11)  
02. <dependency>
03. <groupId>org.testng</groupId>
04. <artifactId>testng</artifactId>
05. <version>5.11</version>
06. <scope>test</scope>
07. <span style="color:#009900;"> <classifier>jdk15</classifier></span>
08. </dependency>

默认会执行的测试用例:*Test.java

7. 如何使用TestNG的 suite

<plugin>  
02. <groupId>org.apache.maven.plugins</groupId>
03. <artifactId>maven-surefire-plugin</artifactId>
04. <version>2.17</version>
05. <configuration>
06. <span style="color:#009900;"><suiteXmlFiles>
07. <suiteXmlFile>testng.xml</suiteXmlFile>
08. </suiteXmlFiles></span>
09. </configuration>
10. </plugin>

注意:This configuration will override the includes and excludes patterns and run all tests in the suite files.

8. 执行群组测试 execute one or more specific groups (Junit & TestNG 通用)

<plugin>  
02. <groupId>org.apache.maven.plugins</groupId>
03. <artifactId>maven-surefire-plugin</artifactId>
04. <version>2.17</version>
05. <configuration>
06. <span style="color:#009900;"> <groups>functest,perftest</groups></span>
07. </configuration>
08. </plugin>

9. 多线程的运行测试用例 Running tests in parallel (Junit & TestNG 通用)

<plugin>  
02. <groupId>org.apache.maven.plugins</groupId>
03. <artifactId>maven-surefire-plugin</artifactId>
04. <version>2.17</version>
05. <configuration>
06. <span style="color:#009900;"><parallel>methods</parallel>
07. <threadCount>10</threadCount></span>
08. </configuration>
09. </plugin>

10. 如何使用Junit Using JUnit

01.<dependency>  
02. <groupId>junit</groupId>
03. <artifactId>junit</artifactId>
04. <version>4.8.1</version>
05. <scope>test</scope>
06. </dependency>

11. 如何使用Junit Category Using JUnit Categories

<plugin>  
02. <artifactId>maven-surefire-plugin</artifactId>
03. <version>2.11</version>
04. <configuration>
05. <span style="color:#009900;"> <strong><groups>com.mycompany.SlowTests</groups></strong></span>
06. </configuration>
07. </plugin>

仅有带该注解的测试 或者 是当前注解 类/接口的 子类 会被执行,This will execute only those tests annotated with the @Category(com.mycompany.SlowTests.class) annotation and
those tests annotated with @Category(com.mycompany.SlowerTests.class) if class/interface SlowerTests is subclass of SlowTests:

public interface SlowTests{}  
02.public interface SlowerTests extends SlowTests{}
03.----------------------------------
04.ic class AppTest {
05. @Test
06. @Category(com.mycompany.SlowTests.class)
07. public void testSlow() {
08. System.out.println("slow");
09. }
10.
11. @Test
12. @Category(com.mycompany.SlowerTests.class)
13. public void testSlower() {
14. System.out.println("slower");
15. }
16.
17. @Test
18. @Category(com.cmycompany.FastTests.class)
19. public void testSlow() {
20. System.out.println("fast");
21. }
22.}

12. 如何debug TestCases

mvnDebug -DforkCount=0 test   dubug非fork的测试  
02.在debug的需求时,还是使用Eclipse最方便

13. 使用系统属性 Using System Properties


01.<build>
02. <plugins>
03. <plugin>
04. <groupId>org.apache.maven.plugins</groupId>
05. <artifactId>maven-surefire-plugin</artifactId>
06. <version>2.17</version>
07. <configuration>
08. <span style="color:#009900;"><systemPropertyVariables>
09. <propertyName>propertyValue</propertyName>
10. <buildDirectory>${project.build.directory}</buildDirectory>
11. [...]
12. </systemPropertyVariables></span>
13. </configuration>
14. </plugin>
15. </plugins>
16. </build>

14. 选择surefire provider

surefire 默认会根据工程的classpath中已有的Junit|TestNG的版本来选择 test-framework provider,我们也可以手动的选择和覆盖当前的provider

<plugin>  
02. <groupId>org.apache.maven.plugins</groupId>
03. <artifactId>maven-surefire-plugin</artifactId>
04. <version>2.17</version>
05. <dependencies>
06. <dependency>
07. <groupId>org.apache.maven.surefire</groupId>
08. <span style="color:#009900;"> <artifactId>surefire-junit47</artifactId></span>
09. <version>2.17</version>
10. </dependency>
11. </dependencies>
12. </plugin>

目前已经提供的provider有surefire-junit3, surefire-junit4, surefire-junit47 and surefire-testng.

   
4766 次浏览       17
     
 
相关文章

项目流程_IPD
EA中的项目管理-计划与跟踪
大型项目中的敏捷项目管理实践
敏捷项目管理概述
 
相关文档

IPD体系框架下的项目管理
项目管理基础与敏捷开发入门
IT项目管理培训
软件项目管理
 
相关课程

软件开发过程中的项目管理
基于IPD的项目管理方法与实践
敏捷项目管理实践
项目管理高级实践
最新课程计划
信息架构建模(基于UML+EA)3-21[北京]
软件架构设计师 3-21[北京]
图数据库与知识图谱 3-25[北京]
业务架构设计 4-11[北京]
SysML和EA系统设计与建模 4-22[北京]
DoDAF规范、模型与实例 5-23[北京]

正视研发管理才是高水平竞争
需求是如何变成产品原型的
产品经理能力模型解说—把控
产品经理的正确定位
谁是合格的产品经理?
产品管理与产品营销的区别
更多...   


统一过程及应用
敏捷过程实践
基于XP/RUP的迭代开发
软件开发过程指南
SCRUM过程实践
敏捷测试-简单而可行


某博彩企业 产品经理与产品管理
北京 研发团队与工作管理
广东金赋信息 敏捷开发过程与项目管理
某支付平台 软件配置管理与发布管理
富士 软件外包项目管理与进度管理
塞孚耐 基于Scrum的敏捷开发
更多...