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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3100924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:27:27  来源:igfitidea点击:

How to run JUnit tests by category in Maven?

javamavenjunitcategoriesmaven-surefire-plugin

提问by Ran

Using JUnit 4.8 and the new @Categoryannotations, is there a way to choose a subset of categories to run with Maven's Surefire plugin?

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

For example I have:

例如我有:

@Test
public void a() {
}

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

And I'd like to run all non-slow tests as in: (note that the -Dtest.categories was made up by me...).

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

So the point is that I don't want to have to create test suites (Maven just picks up all unit tests in the project which is very convenient) and I'd like Maven to be able to pick the tests by category. I think I just made up the -Dtest.categories, so I was wondering if there's a similar facility I can use?

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

回答by Jesus Benito

Not exactly the same thing but using surefire plugin, test classes can be chosen based on file name. You are not using Junit Categories though.

不完全相同,但使用surefire插件,可以根据文件名选择测试类。不过,您没有使用 Junit 类别。

An example for running just DAO tests.

仅运行 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

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

回答by dmcnelis

Maven has since been updated and can use categories.

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

An example from the Surefire documentation:

Surefire 文档中的一个示例

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

This will run any class with the annotation @Category(com.mycompany.SlowTests.class)

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

回答by Mark Butler

Based on this blog post- and simplifying - add this to your pom.xml:

基于此博客文章- 并简化 - 将其添加到您的 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>

then at the command line

然后在命令行

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

回答by paucls

You can use

您可以使用

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

But ensure that you configure properly the maven surefire plugin

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

See docs in: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

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

回答by Joel

I had a similar case where I want to run all test EXCEPT a given category (for instance, because I have hundreds of legacy uncategorized tests, and I can't / don't want to modify each of them)

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

The maven surefire plugin allows to exclude categories, for instance:

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>

回答by Remy

I lost lot of time on this error "groups/excludedGroups require TestNG or JUnit48+ on project test classpath" because I thought I was using a bad version of junit, or a bad version of the surefire plugin, or a combination that does not fit.

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

It was none of that: in my project I had a "config" module that was built before the module I wanted to test. This module had no junit dependency -> it had no junit on the classpath...

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

This mistake may help others...

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