java - 如何在 Maven 中的 exceptedGroups 中运行单元测试

标签 java maven unit-testing junit

我有一个 JUnit 4.12 SlowTests 测试套件,除非在 Maven 命令行上特别请求,否则我希望将其从执行中排除。

我已将以下内容添加到我的 pom 文件中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <configuration>
        <excludedGroups>com.Example.SlowTests</excludedGroups>
        <includes>
            <include>**/*TestSuite.class</include>
        </includes>
        <excludes>
            <exclude></exclude>
        </excludes>
    </configuration>
</plugin>

我已将类别定义为 SlowTests 并将其应用于 MySlowTests 类。

我对测试套件进行了注释,如下所示:

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses({ MySlowTests.class })
public class MySlowTestSuite

当我运行mvn package时,除了MySlowTests之外的所有单元测试都会被执行。

但是,查看各种答案,例如 https://stackoverflow.com/a/25639372/820657https://stackoverflow.com/a/21830866/820657我期望以下命令:

mvn package -Dgroups=com.Example.MySlowTests

运行排除的 MySlowTests 测试,但它们不运行。事实上没有测试运行。

我做错了什么?

最佳答案

Maven Surefire 插件在版本 < 2.13 (IIRC) 中存在一些关于类别的问题,但只要您使用 Surefire >= 2.13,以下内容将运行任何用 @Category(com.yourcompany.SlowTests.类):

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.13</version>
      <configuration>
        <groups>com.yourcompany.SlowTests</groups>
      </configuration>
</plugin>

这种方法经常与配置文件一起使用,以下配置...

<profiles>
    <profile>
        <id>slow</id>
        <properties>
            <testCategories>com.yourcompany.SlowTests</testCategories>
        </properties>
    </profile>
    <profile>
        <id>fast</id>
        <properties>
            <testCategories>com.yourcompany.FastTests</testCategories>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.13</version>
            <configuration>
                <groups>${testCategories}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

...可用于运行:

  • mvn install -P Slow:仅运行慢速测试
  • mvn install -P fast:仅运行快速测试
  • mvn install -P fast,slow:运行快速和慢速测试

更新 1: 对于这个问题:“有没有办法使用这种方法,以便我可以默认运行所有快速测试?”

您可以定义两个属性:

<properties>
    <includedCategories></includedCategories>
    <excludedCategories>com.yourcompany.SlowTests</excludedCategories>
</properties>

然后更新您的 Surefire 插件定义,如下所示:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.13</version>
        <configuration>
            <groups>${includedCategories}</groups>
            <excludedGroups>${excludedCategories}</excludedGroups>
        </configuration>
    </plugin>

最后,添加此配置文件:

    <profile>
        <id>slow</id>
        <properties>
            <includedCategories>com.yourcompany.SlowTests</includedCategories>
            <excludedCategories></excludedCategories>
        </properties>
    </profile>

这只是切换 includedCategoriesexcludedCategories 属性。默认情况下,您包含除了那些用com.yourcompany.SlowTests注释的测试之外的所有内容(即除了“慢速”测试之外的所有内容)。当您使用 -P Slow 运行时,您会排除除了用 com.yourcompany.SlowTests 注释的测试之外的所有内容(即除了“慢速”测试之外的所有内容) )。

注意:我在原来的答案中所说的关于 Surefire 版本 < 2.13 与类别的不当行为仍然有效,因此要使这项工作正常进行,您需要使用 >= 2.13 的 Maven Surefire 插件版本。

关于java - 如何在 Maven 中的 exceptedGroups 中运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46177921/

相关文章:

java - 代码可以编译并运行,但有一部分无法正确循环

maven - Liquibase 与 Maven

java - 重复传递性使用的依赖项?

c# - 仅保护条款需要单元测试吗?

java - 尝试使用 Java 从文本中删除 html

扩展实例的Java架构问题(需要模式推荐)

java - 如何将数组中某个变量的每个实例设置为 null 并将每个变量向左移动

spring - 如何避免将特定版本的 Spring Boot 放入库中

javascript - Karma + PhantomJS TypeError : undefined is not an object (evaluating scope. 大奖)

PHPUnit模拟父方法