如何在Maven中按类别运行JUnittesting?

使用JUnit 4.8和新的@Category注解,有没有办法selectMaven的Surefire插件运行的类别的子集?

例如,我有:

@Test public void a() { } @Category(SlowTests.class) @Test public void b() { } 

我想运行所有非慢速testing,如下所示(注意-Dtest.categories由我组成)。

 mvn test -Dtest.categories=!SlowTests // run non-slow tests mvn test -Dtest.categories=SlowTests // run only slow tests mvn test -Dtest.categories=SlowTests,FastTests // run only slow tests and fast tests mvn test // run all tests, including non-categorized 

所以问题是我不想创buildtesting套件(Maven只是在项目中拿起所有的unit testing非常方便),我希望Maven能够按类别selecttesting。 我想我只是编写了-Dtest.categories,所以我想知道是否有类似的设施可以使用?

Maven已经更新并可以使用类别。

来自Surefire文档的一个例子:

 <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.11</version> <configuration> <groups>com.mycompany.SlowTests</groups> </configuration> </plugin> 

这将运行任何类与注释@Category(com.mycompany.SlowTests.class)

基于这个博客文章 – 简化 – 添加到您的pom.xml中:

 <profiles> <profile> <id>SlowTests</id> <properties> <testcase.groups>com.example.SlowTests</testcase.groups> </properties> </profile> <profile> <id>FastTests</id> <properties> <testcase.groups>com.example.FastTests</testcase.groups> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.13</version> </dependency> </dependencies> <configuration> <groups>${testcase.groups}</groups> </configuration> </plugin> </plugins> </build> 

然后在命令行

 mvn install -P SlowTests mvn install -P FastTests mvn install -P FastTests,SlowTests 

我有一个类似的情况,我想运行所有的testing除了一个给定的类别(例如,因为我有数以百计的遗留未分类testing,我不能/不想修改每个)

maven surefire插件允许排除类别,例如:

 <profiles> <profile> <id>NonSlowTests</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludedGroups>my.category.SlowTest</excludedGroups> </configuration> </plugin> </plugins> </build> </profile> </profiles> 

您可以使用

 mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests" 

但是请确保您正确configurationmaven surefire插件

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.11</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.12.2</version> </dependency> </dependencies> </plugin> 

请参阅以下文档: https : //maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

我在这个错误上丢失了很多时间“groups / excludedGroups需要在项目testing类path上使用TestNG或JUnit48 +”,因为我以为我使用的是junit的糟糕版本,surefire插件的错误版本或者不适合的组合。

不是这样的:在我的项目中,我有一个“configuration”模块,它是在我想testing的模块之前构build的。 这个模块没有junit依赖关系 – >它在类path上没有junit …

这个错误可能会帮助别人…

不完全一样的东西,但使用surefire插件,testing类可以根据文件名select。 尽pipe你没有使用Junit类别。

运行DAOtesting的一个例子。

 <executions> <execution> <id>test-dao</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <excludes> <exclude>none</exclude> </excludes> <includes> <include>**/com/proy/core/dao/**/*Test.java</include> </includes> </configuration> </execution> 

http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html