如何在Maven中的excludedGroups中运行单元测试

问题描述:

我有一个JUnit 4.12 SlowTests测试套件,我想从执行中排除它,除非在Maven命令行中有特别的要求。如何在Maven中的excludedGroups中运行单元测试

我加入以下到我的POM文件:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.19</version> 
    <configuration> 
     <excludedGroups>com.Example.SlowTests</excludedGroups> 
     <includes> 
      <include>**/*TestSuite.class</include> 
     </includes> 
     <excludes> 
      <exclude></exclude> 
     </excludes> 
    </configuration> 
</plugin> 

我已经定义的类别为SlowTests,并把它应用到MySlowTests类。

我已注释的测试套件如下:

@RunWith(Categories.class) 
@IncludeCategory(SlowTests.class) 
@SuiteClasses({ MySlowTests.class }) 
public class MySlowTestSuite 

当运行mvn package所有除MySlowTests单元测试被执行。

然而,寻找不同的答案,如https://*.com/a/25639372/820657https://*.com/a/21830866/820657我希望下面的命令:

mvn package -Dgroups=com.Example.MySlowTests 

运行排除MySlowTests测试,但它们不运行。实际上没有测试运行。

我在做什么错?

Maven的Surefire插件在版本< 2.13(IIRC)的一些问题WRT类,但只要使用的是神火> = 2.13以下运行的类注解为@Category(com.yourcompany.SlowTests.class)

<plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.13</version> 
     <configuration> 
     <groups>com.yourcompany.SlowTests</groups> 
     </configuration> 
</plugin> 

这种方法经常使用配置文件,下面的配置使用...

<profiles> 
    <profile> 
     <id>slow</id> 
     <properties> 
      <testCategories>com.yourcompany.SlowTests</testCategories> 
     </properties> 
    </profile> 
    <profile> 
     <id>fast</id> 
     <properties> 
      <testCategories>com.yourcompany.FastTests</testCategories> 
     </properties> 
    </profile> 
</profiles> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.13</version> 
      <configuration> 
       <groups>${testCategories}</groups> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

...可以用来运行:

  • mvn install -P slow:运行速度慢的测试只
  • mvn install -P fast:运行快速测试仅
  • mvn install -P fast,slow:运行快,慢测试

更新1:这个问题:“有一种使用这种方法的方法,所以我可以默认运行所有快速测试?“

您可以定义两个属性:

<properties> 
    <includedCategories></includedCategories> 
    <excludedCategories>com.yourcompany.SlowTests</excludedCategories> 
</properties> 

像这样然后更新您的神火插件定义:

<plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.13</version> 
     <configuration> 
      <groups>${includedCategories}</groups> 
      <excludedGroups>${excludedCategories}</excludedGroups> 
     </configuration> 
    </plugin> 

最后,添加该配置文件:

<profile> 
     <id>slow</id> 
     <properties> 
      <includedCategories>com.yourcompany.SlowTests</includedCategories> 
      <excludedCategories></excludedCategories> 
     </properties> 
    </profile> 

这只是切换includedCategoriesexcludedCategories属性。默认情况下,您的包括除了那些用com.yourcompany.SlowTests(即除了'慢'测试以外的所有内容)。当您使用-P slow运行时,您不包括除了那些用com.yourcompany.SlowTests注释的测试(即除'慢'测试之外的所有测试)之外的所有内容。

注意:我在原始答案中提到的有关Surefire版本<的问题2.13使用类别行为异常现象仍然存在,因此您需要使用Maven Surefire插件版本> = 2.13。

+0

有没有办法使用这种方法,所以我可以默认运行所有快速测试?我不想对所有的快速测试套件进行注释。 – ksl

+0

@ksl我已经更新了包含该要求的实现细节的答案。 – glytching