我有一个junit 4.12 SlowTests
测试套件,除非在maven命令行上明确要求。
我已将以下内容添加到我的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://stackoverflow.com/a/a/25639372/820657和https://stackoverflow.com/a/a/a/21830866/820657我期望以下命令:
mvn package -Dgroups=com.Example.MySlowTests
运行排除的MySlowTests
测试,但它们不运行。实际上没有测试。
我在做什么错?
maven surefire插件在版本&lt; lt;2.13(iirc),但只要您使用surefire> = 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>
然后更新您的SureFire插件定义:
<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>
这只需切换includedCategories
和excludedCategories
属性即可。默认情况下,您 include 除了用com.yourcompany.SlowTests
注释的测试以外的所有内容(即除了您的"慢"测试之外的所有内容)。当您使用-P slow
运行时,您排除除了用com.yourcompany.SlowTests
注释的测试以外的所有内容(即除了您的"慢"测试以外的所有内容)。
注意:我在有关Surefire版本的原始答案中所说的话;2.13与类别的不当行为仍然存在,因此要使这项工作您需要使用Maven Surefire插件的版本> = 2.13。