77

使用 JUnit 4.8 和新的@Category注释,有没有办法选择一个类别的子集来运行 Maven 的 Surefire 插件?

例如我有:

@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,所以我想知道是否有类似的工具可以使用?

4

9 回答 9

77

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)

于 2012-02-07T15:03:42.377 回答
50

基于此博客文章- 并简化 - 将其添加到您的 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
于 2013-08-18T09:14:43.937 回答
24

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

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>
于 2014-09-03T08:10:58.190 回答
12

您可以使用

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

但请确保您正确配置了 maven 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

于 2014-02-17T13:51:52.057 回答
6

我在这个错误“groups/excludedGroups require TestNG or JUnit48+ on project test classpath”上浪费了很多时间,因为我认为我使用了错误版本的 junit,或者错误版本的 surefire 插件,或者不适合的组合。

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

这个错误可能会帮助其他人......

于 2015-05-19T12:17:41.500 回答
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

于 2011-03-30T13:59:29.453 回答
1

我在使用类别运行测试时遇到问题,例如:@Category(com.mycompany.SlowTests.class)通过运行测试时mvn test -Dgroups=com.mycompany.SlowTests.class

我发现Test名称中没有单词的类中的测试将无法运行。将单词添加Test到类后,类中的测试运行。

于 2020-06-15T21:27:29.790 回答
1

对于您希望默认忽略某些测试然后有条件地从命令行运行所有测试的用例,此设置将适用于JUnit 4.9.

首先创建标记界面:

public interface IntegrationTest {}

注释要排除的测试:

@Category(IntegrationTest.class)
public class MyIntegrationTest() {}

将插件和配置文件添加到pom.xml

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <!-- Read JUnit categories to be excluded from Maven property -->
    <configuration>
      <excludedGroups>${test.excluded.groups}</excludedGroups>
    </configuration>
  </plugin>
</plugins>
  
<profiles>
  <profile>
    <id>Default</id>
    <!-- Set profile to be active by default -->
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <test.excluded.groups>com.example.IntegrationTest</test.excluded.groups>
    </properties>
  </profile>
  <profile>
    <id>AllTest</id>
    <!-- Set groups property to blank value which will match nothing -->
    <properties>
      <test.excluded.groups></test.excluded.groups>
    </properties>
  </profile>
</profiles>

从命令行照常运行测试时,将排除集成测试:

mvn test

包括集成测试在内的所有测试都可以使用相应的配置文件激活:

mvn test -P AllTest
于 2021-01-11T20:52:43.460 回答
0

Junit 5 允许您使用 @Tag 注释。有关此处的更多信息: https ://www.baeldung.com/junit-filtering-tests

我发现它看起来更干净一些:

@Tag("SlowTests")
@Test
public void b() {
}
于 2020-06-23T20:45:06.090 回答