如何在Maven中按类别运行JUnit测试?

Ran*_*Ran 70 java junit categories maven maven-surefire-plugin

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

例如,我有:

@Test
public void a() {
}

@Category(SlowTests.class)
@Test
public void b() {
}
Run Code Online (Sandbox Code Playgroud)

我想按照以下方式运行所有非慢速测试:(注意-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
Run Code Online (Sandbox Code Playgroud)

所以重点是我不想创建测试套件(Maven只是在项目中选择了非常方便的所有单元测试)并且我希望Maven能够按类别选择测试.我想我刚编了-Dtest.categories,所以我想知道是否有类似的设施我可以使用?

dmc*_*lis 74

Maven已经更新,可以使用类别.

Surefire文档中的一个示例:

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
        <groups>com.mycompany.SlowTests</groups>
      </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

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

  • 版本2.11对我不起作用,因为Surefire一直忽略我指定的组.升级到surefire插件版本2.12.3就可以了 (8认同)
  • 但是如何为不同的类别进行多种配置?即OP希望能够在命令行上指定类别,但是如果在POM中指定了类别,那么如何在命令行中指定类别? (7认同)
  • 还有 `excludedGroups` 选项,请参阅:http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#excludedGroups (3认同)

Mar*_*ler 45

基于此博客文章 - 并简化 - 将其添加到您的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>
Run Code Online (Sandbox Code Playgroud)

然后在命令行

mvn install -P SlowTests
mvn install -P FastTests
mvn install -P FastTests,SlowTests
Run Code Online (Sandbox Code Playgroud)

  • 运行两个配置文件togather不适合我 (3认同)

Joe*_*oel 21

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

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>
Run Code Online (Sandbox Code Playgroud)


小智 9

您可以使用

mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests"
Run Code Online (Sandbox Code Playgroud)

但请确保正确配置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>
Run Code Online (Sandbox Code Playgroud)

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


Rem*_*emy 5

我在此错误“项目组/被排除的组在项目测试类路径上需要TestNG或JUnit48 +”上浪费了很多时间,因为我以为我使用的是junit的错误版本,surefire插件的错误版本或不合适的组合。

没什么:在我的项目中,我有一个“ config”模块,该模块在要测试的模块之前构建。该模块没有junit依赖关系->在类路径上没有junit ...

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


Jes*_*ito 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>
Run Code Online (Sandbox Code Playgroud)

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