`
xinklabi
  • 浏览: 1579437 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

Maven中测试插件(surefire)的相关配置及常用方法

 
阅读更多

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

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

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.   <groupId>org.apache.maven.plugins</groupId>  
  3.   <artifactId>maven-surefire-plugin</artifactId>  
  4.   <version>2.17</version>  
  5. </plugin>  

 

2. 默认被执行的测试
   
   默认情况下,surefire会执行文件名以Test开头或结尾的测试用例,或者是以TestCase结尾的测试用例。
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. "**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".  
  2. "**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".  
  3. "**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".</span>  
3. 跳过测试   Skipping Tests
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.        <span style="color:#009900;"> <configuration>  
  8.           <skipTests>true</skipTests>  
  9.         </configuration></span>  
  10.       </plugin>  
  11.     </plugins>  
  12.   </build>  
  13.   
  14. 补充:如果使用Junit或者TestNG也可以直接在当前测试方法上加注解@Ignore忽略,这是加了该注解的也都会被skip  
4. 排除测试 Exclusions  (Junit & TestNG 通用)
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.         <configuration>  
  8.          <span style="color:#009900;"> <excludes>  
  9.             <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    如果测试类没有使用规范的命名,可以显示的直接指定测试方法的名称

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. Running a set of methods in a Single Test Class  
  2.   
  3. With version 2.7.3, you can run only n tests in a single Test Class.  
  4.  NOTE : it's supported for junit 4.x and TestNG.   
  5. You must use the following syntax  
  6. mvn -Dtest=TestCircle#mytest test  
  7. You can use patterns too  
  8. mvn -Dtest=TestCircle#test* test  
  9. 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

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <dependency>  
  2.       <groupId>org.testng</groupId>  
  3.       <artifactId>testng</artifactId>  
  4.       <version>6.3.1</version>  
  5.       <scope>test</scope>  
  6.     </dependency>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. If  using an older version of TestNG (<= 5.11)  
  2.     <dependency>  
  3.       <groupId>org.testng</groupId>  
  4.       <artifactId>testng</artifactId>  
  5.       <version>5.11</version>  
  6.       <scope>test</scope>  
  7.      <span style="color:#009900;"> <classifier>jdk15</classifier></span>  
  8.     </dependency>  

 

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

7. 如何使用TestNG的 suite

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.         <groupId>org.apache.maven.plugins</groupId>  
  3.         <artifactId>maven-surefire-plugin</artifactId>  
  4.         <version>2.17</version>  
  5.         <configuration>  
  6.           <span style="color:#009900;"><suiteXmlFiles>  
  7.                 <suiteXmlFile>testng.xml</suiteXmlFile>  
  8.             </suiteXmlFiles></span>  
  9.         </configuration>  
  10.       </plugin>  

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

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

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

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.        <groupId>org.apache.maven.plugins</groupId>  
  3.        <artifactId>maven-surefire-plugin</artifactId>  
  4.        <version>2.17</version>  
  5.        <configuration>  
  6.         <span style="color:#009900;"> <groups>functest,perftest</groups></span>  
  7.        </configuration>  
  8.      </plugin>  


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

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.         <groupId>org.apache.maven.plugins</groupId>  
  3.         <artifactId>maven-surefire-plugin</artifactId>  
  4.         <version>2.17</version>  
  5.         <configuration>  
  6.           <span style="color:#009900;"><parallel>methods</parallel>  
  7.           <threadCount>10</threadCount></span>  
  8.         </configuration>  
  9.       </plugin>  


10. 如何使用Junit  Using JUnit

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <dependency>  
  2.       <groupId>junit</groupId>  
  3.       <artifactId>junit</artifactId>  
  4.       <version>4.8.1</version>  
  5.       <scope>test</scope>  
  6.     </dependency>  

11. 如何使用Junit Category    Using JUnit Categories

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.       <artifactId>maven-surefire-plugin</artifactId>  
  3.       <version>2.11</version>  
  4.       <configuration>  
  5.        <span style="color:#009900;"> <strong><groups>com.mycompany.SlowTests</groups></strong></span>  
  6.       </configuration>  
  7.     </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:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public interface SlowTests{}  
  2. public interface SlowerTests extends SlowTests{}  
  3. ------------------------------------------------------------------------------------------------  
  4. ic class AppTest {  
  5.   @Test  
  6.   @Category(com.mycompany.SlowTests.class)  
  7.   public void testSlow() {  
  8.     System.out.println("slow");  
  9.   }  
  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. }  

参考:

Junit的@Category详解

 

 

12. 如何debug TestCases

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. mvnDebug -DforkCount=0 test   dubug非fork的测试  
  2. 在debug的需求时,还是使用Eclipse最方便  


13. 使用系统属性    Using System Properties

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.apache.maven.plugins</groupId>  
  5.         <artifactId>maven-surefire-plugin</artifactId>  
  6.         <version>2.17</version>  
  7.         <configuration>  
  8.           <span style="color:#009900;"><systemPropertyVariables>  
  9.             <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
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-surefire-plugin</artifactId>  
  4.     <version>2.17</version>  
  5.     <dependencies>  
  6.       <dependency>  
  7.         <groupId>org.apache.maven.surefire</groupId>  
  8.        <span style="color:#009900;"> <artifactId>surefire-junit47</artifactId></span>  
  9.         <version>2.17</version>  
  10.       </dependency>  
  11.     </dependencies>  
  12.   </plugin>  
目前已经提供的provider有surefire-junit3, surefire-junit4, surefire-junit47 and surefire-testng
参考:http://mvnrepository.com/artifact/org.apache.maven.surefire
注意:选择手动指定provider时,不要忘记安装test framework
15. class Loading issues
please refer to:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html



 

原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong

分享到:
评论

相关推荐

    Maven学习必要插件

    在本文中,我们将深入探讨Maven的核心功能、常用插件以及如何配置和使用这些插件。 1. **Maven核心功能** Maven通过Project Object Model (POM)来描述项目,POM是一个XML文件,包含了项目的配置信息,如依赖、构建...

    Maven常用插件整理.docx

    Surefire Plugin是Maven的默认单元测试插件,用于运行项目中的JUnit或其他兼容测试框架的测试。开发者可以通过配置控制测试的执行,如选择特定的测试套件或排除某些测试。 5. Maven Resources Plugin Resources ...

    maven插件及其使用方法

    在“maven插件及其使用方法(1)”这个文件中,可能包含了更具体的插件使用示例和实战教程,涵盖了如何配置插件、如何自定义插件目标等细节。通过学习这些内容,开发者可以更加熟练地驾驭Maven,提升开发效率,确保...

    Eclipse中Maven插件

    8. **插件管理**:Maven的强大之处在于其丰富的插件生态系统,通过POM文件可以配置使用特定的Maven插件,如Surefire Plugin用于运行单元测试,Failsafe Plugin用于集成测试。Eclipse插件提供了方便的插件管理界面,...

    maven常用命令以及以个测试项目

    ### Maven测试项目 在“test”这个文件中,通常包含的是Maven项目的测试部分,比如JUnit测试或者Mockito等测试框架的代码。这些测试文件位于`src/test/java`目录下,遵循Maven的标准目录结构。编写好测试后,可以...

    maven插件压缩包

    如果你在 Eclipse 中使用 Maven,可以通过以下步骤配置离线 Maven 插件: 1. 打开 Eclipse,选择 `Window` -&gt; `Preferences` -&gt; `Maven` -&gt; `User Settings`。 2. 在 `User Settings` 窗口中,设置 `Maven Home ...

    Maven3实战笔记04Maven的生命周期和插件

    4. **test**:执行Default生命周期中的`test`阶段,调用`maven-surefire-plugin`的`test`目标来运行单元测试。 5. **package**:执行Default生命周期中的`package`阶段,调用`maven-jar-plugin`的`jar`目标来打包...

    maven常用包

    在"maven常用包"这个主题中,我们主要探讨的是Maven生态系统中的核心概念、重要组件以及常用的插件和配置。 一、Maven核心概念 1. 项目对象模型(Project Object Model,POM):Maven的中心概念,它是一个XML文件,...

    MAVEN-配置apache-maven-3.5.2.zip

    在本文中,我们将深入探讨"MAVEN-配置apache-maven-3.5.2.zip"的相关知识点。 1. **Maven的安装与配置** - 下载:Apache Maven 3.5.2是Maven的一个稳定版本,用户可以从Apache官方网站下载这个zip文件。 - 解压:...

    maven插件及其使用方法(2)

    以上只是对Maven插件及其使用方法的一个简单概述,实际开发中,开发者可以根据项目需求选择和配置合适的插件,以提高构建效率和项目管理的便捷性。通过学习和实践,你将能够熟练掌握Maven插件的使用,为Java项目带来...

    maven安装包和仓库

    Maven 插件扩展了 Maven 的功能,例如 `maven-compiler-plugin` 用于编译,`maven-surefire-plugin` 用于执行测试。通过在 `pom.xml` 中配置插件,可以实现自定义的构建步骤。 ### 六、Maven 依赖管理 Maven 使用...

    Maven 插件

    而Maven插件则是Maven生态系统中的重要组成部分,它们提供了执行特定任务的功能,如编译代码、运行测试、打包应用等。 1. **什么是Maven插件** Maven插件是Maven生命周期的一部分,它们是一系列目标(Goals)的...

    maven的配置

    Maven的插件系统允许扩展其功能,例如,`maven-surefire-plugin`用于执行单元测试,`maven-jar-plugin`用于创建JAR文件,`maven-war-plugin`用于Web应用的打包。 通过以上配置,你将拥有一个基本的Maven工作环境,...

    maven工具包 maven plugins

    Maven插件是Maven生态系统中的重要部分,它们扩展了Maven的功能,使得开发者能够执行特定的任务,如资源复制、打包、部署、测试等。Maven插件通过在`pom.xml`文件中配置来启用,例如: ```xml &lt;groupId&gt;org....

    jacoco单元测试覆盖率+maven DEMO2

    它的优势在于简单易用,只需在Maven项目中添加相应的插件配置,即可在构建过程中自动收集覆盖率数据,并生成易于理解的报告。 **JaCoCo与Maven集成** 要在Maven项目中使用JaCoCo,首先需要在项目的`pom.xml`文件中...

    Eclipse安装Maven插件(可参照文档)

    在IT行业中,集成开发环境(IDE)如Eclipse是程序员常用的工具,而Maven作为Java项目管理和构建的利器,能够帮助我们自动化构建、依赖管理以及项目信息管理。本篇文章将详细讲解如何在Eclipse中安装Maven插件,以便...

    Maven总结【含配置文件】

    Maven 插件扩展了 Maven 的功能,如 `maven-compiler-plugin` 用于编译Java源代码,`maven-surefire-plugin` 用于执行单元测试。插件通过 `&lt;build&gt;&lt;plugins&gt;` 部分配置,并可以自定义其执行目标和参数。 ** Maven ...

    maven in action 中文

    书中会介绍如何配置和使用各种常用插件,如javadoc插件、surefire插件和maven-assembly-plugin。 除了基本功能,本书还涵盖了Maven的高级话题,如多模块项目管理、远程仓库的使用、解决依赖冲突、自定义构建过程...

    Mybatis+Maven插件

    4. **Maven插件**:Maven的插件机制十分强大,如`maven-compiler-plugin`用于编译Java源代码,`maven-surefire-plugin`负责运行单元测试。 结合Mybatis和Maven,我们可以使用Mybatis Generator插件来自动化生成DAO...

    maven介绍及配置详解

    ### Maven介绍及配置详解 #### 一、Maven概述 Maven是由Apache软件基金会支持的一款项目管理和理解工具,主要用于Java项目的构建与管理。Maven通过一个中心信息来管理项目的构建、报告和文档,大大简化了Java项目...

Global site tag (gtag.js) - Google Analytics