2010-06-23 116 views
54

使用JUnit 4.8和新的@Category标注,有没有办法选择Maven的Surefire插件运行的类别子集?如何在Maven中按类别运行JUnit测试?

比如我有:

@Test 
public void a() { 
} 

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

而且我想运行所有非慢速测试,如下所示:(注意-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 

所以问题是,我不希望有创建测试套件(Maven,那么拿起所有的单元测试中,这是非常方便的项目),我想Maven来能够挑选按类别进行测试。 我想我只是编制了-Dtest.categories,所以我想知道是否有类似的设施可以使用?

+0

参见[TestNG的混合和JUnit(http://stackoverflow.com/questions/9172820/mix-testng-junit-with-maven-surefire-plugin-version-2-11-and-higher-for-integ)了解更多详情。从maven-surefire-plugin 2.11版支持类别 – 2012-02-29 13:50:48

回答

1

不完全一样的东西,但使用surefire插件,可以根据文件名选择测试类。尽管你没有使用Junit类别。

一个只运行DAO测试的例子。

<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

67

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

Surefire documentation:

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

一个例子这将运行任何类注释@Category(com.mycompany.SlowTests.class)

+7

版本2.11对我不起作用,因为Surefire一直忽略我指定的组。升级到surefire插件版本2.12.3的窍门 – 2012-09-12 22:11:58

+5

尽管如何为不同的类别做多种配置?即OP希望能够在命令行上指定一个类别,但是如果在POM中指定了该类别,那么如何在命令行中指定一个类别? – 2013-02-26 16:50:22

+0

注意!版本2.11忽略组!在http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html#Using_JUnit_Categories下提供的示例仍然不正确! – Heezer 2013-04-24 12:39:07

38

在此基础上blog post - 和简化 - 添加到您的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 
+0

surefire插件的2.13版本不适合我。我得到了这个错误:“组/ excludedGroups需要项目测试类路径上的TestNG或JUnit48 +”,尽管我使用的是Junit 4.11。将surefire插件降级到2.12.2解决了错误。 – 2014-03-27 11:48:54

+0

这对我来说,使用Surefire 2.17版本。我必须将org.apache.maven.surefire:surefire-junit47:2.13依赖项更改为org.apache.maven.surefire:common-junit48:2.17。 – Kkkev 2014-05-12 10:55:19

+0

运行两个配置文件togather没有为我工作 – 2018-01-16 14:06:53

5

您可以使用

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

但确保您配置正确的行家万无一失插件

<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

16

我有我想要的类似案例运行除给定类别之外的所有测试(例如,因为我有数百个遗留的未分类测试,并且我不能/不希望修改它们中的每一个)

Maven的万无一失插件允许排除类别,例如:

<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> 
+0

热你会设置只运行一个特定的组? – 2Big2BeSmall 2016-08-10 13:38:31

+1

@Francy只是运行相应的配置文件,从这个例子:mvn test -P NonSlowTests – Joel 2016-08-30 13:01:40

+0

你能否排除多个组? – 2016-11-10 01:52:42

3

我失去了很多时间在这个错误“团体/ excludedGroups需要TestNG的或JUnit48 +项目测试类路径”,因为我以为我所用junit的糟糕版本,surefire插件的错误版本或者不适合的组合。

这并非如此:在我的项目中,我有一个“配置”模块,它是在我想测试的模块之前构建的。该模块没有JUnit的依赖 - >它必须在类路径中没有junit的...

这个错误可以帮助别人......

相关问题