unit-testing - 在 Maven 中组合配置文件以跳过多个类别的单元测试?

标签 unit-testing maven maven-profiles

我在尝试配置 Maven 以排除基于多个配置文件的某些类别的单元测试时遇到问题。

我为单元测试定义了两个类别:SlowTest对于那些需要很长时间才能运行的单元测试,WindowsTest对于那些只能在 Windows 环境中执行的单元测试。

我在项目的 pom.xml 中定义了两个配置文件文件如下:

<profiles>
  <!-- Exclude any tests in `SlowTest` category when profile 'skipSlowTests' is specified. -->
  <profile>
    <id>skipSlowTests</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <excludedGroups>SlowTest.class</excludedGroups>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>  

  <!-- Skip any tests in 'WindowsTest' category when not running on Windows. -->
  <profile>
    <id>skipWindowsTests</id>
    <activation>
      <os><family>!windows</family></os>
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <excludedGroups>WindowsTest.class</excludedGroups>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>  

</profiles>

因此,例如,当我想运行测试并排除任何慢速测试时,我可以执行 mvn -PskipSlowTests test .如果我想跳过任何 Windows 测试,我可以执行 mvn test在非 Windows 操作系统上(或在命令行上明确指定 -PskipWindowsTests)。

这里的问题是当skipSlowTestsskipWindowsTests配置文件被激活,然后只跳过 Windows 测试。缓慢的测试仍在运行。当maven构造有效pom.xml文件,它应用 skipSlowTests配置文件和配置maven-surefire-plugin排除组 SlowTest.class .然后 skipWindowsTests应用配置文件,这会将排除的组覆盖为 WindowsTest.class .

我真正想要的是被排除的组是SlowTest.class,WindowsTest.class当两个配置文件都被激活时,但我想不出办法来做到这一点。

我无法在 Maven 中看到将值附加到属性,例如
<properties>
  <excludedGroups>IgnoreTest.class</excludedGroups>
</properties>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludedGroups>${excludedGroups}</excludedGroups>
  </configuration>
</plugin>

<profile>
  <id>skipSlowTests</id>
  <property>
    <excludedGroups>${excludedGroups},SlowTest.class</excludedGroups>
  </property>
  ...
</profile>

<profile>
  <i>skipWindowsTests</id>
  <property>
    <excludedGroups>${excludedGroups},WindowsTest.class</excludedGroups>
  </property>
  ...
</profile>

这个pom.xml已损坏,因为在为 excludedGroups 赋值时存在递归属性(property)。

并且没有办法设置第三个配置文件(例如 skipSlowTestsAndWindowsTests ),只要 skipSlowTestsskipWindowsTests被激活。

这里有什么建议吗?也许将单独的阶段添加到 maven-surefire-plugin (一个用于慢速测试,一个用于 Windows 测试,一个用于所有其他测试)并使用配置文件来确定运行哪些阶段?

编辑#1

我尝试为 maven-surefire-plugin 创建单独的执行阶段如下:
<!-- By default, do not skip slow tests or windows tests -->
<properties>
  <skipSlowTests>false</skipSlowTests>
  <skipWindowsTests>false</skipWindowsTests>
</properties>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <executions>
    <!-- Default execution will skip SlowTest and WindowsTest categories -->
    <execution>
      <id>default-test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <excludedGroups>SlowTest,WindowsTest</excludedGroups>
      </configuration>
    </execution>
    <!-- Slow Test execution will run the slow tests only (if not skipped) -->
    <execution>
      <id>slow-test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <skip>${skipSlowTests}</skip>
        <groups>SlowTest</groups>
      </configuration>
    </execution>
    <!-- Windows Test execution will run the windows tests only (if not skipped) -->
    <execution>
      <id>windows-test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <skip>${skipWindowsTests}</skip>
        <groups>WindowsTest</groups>
      </configuration>
    </execution>
  </executions>
</plugin>

<profiles>
  <!-- Skip slow tests when skipSlowTests profile enabled -->
  <profile>
    <id>skipSlowTests</id>
    <properties>
      <skipSlowTests>true</skipSlowTests>
    </properties>
  </profile>
  <!-- Skip windows tests when skipWindowsTests profile enabled -->
  <profile>
    <id>skipWindowsTests</id>
    <properties>
      <skipWindowsTests>true</skipWindowsTests>
    </properties>
    ...
  </profile>
</profiles>

此配置将在三个单独的执行中运行测试:default-test执行将运行除慢速测试和 Windows 测试之外的所有测试; slow-test执行将只运行慢速测试;和 windows-test执行将仅运行 Windows 测试。如果两个 skipSlowTestsskipWindowsTests配置文件被启用,那么慢速测试和 Windows 测试都将被跳过。

这里的问题是当有一个测试标记为 SlowTestWindowsTest类别:
@Test @Category({SlowTest.class,WindowsTest.class})
public void slowWindowsTestCase() { ... }

skipSlowTests仅启用配置文件,然后 windows-test执行仍在运行,并且由于 slowWindowsTestCase方法被标记为 windows 测试,它会被执行,即使我们想跳过慢速测试。同样,这个测试用例也会在 skipWindowsTests 时执行。仅配置文件已启用。跳过此测试用例的唯一方法是同时指定 skipSlowTestsskipWindowsTests配置文件。

我尝试如下修改执行:
    <execution>
      <id>slow-test</id>
        ... 
        <groups>SlowTest</groups>
        <excludedGroups>WindowsTest</excludedGroups>
        ...
    </execution>
    <execution>
      <id>windows-test</id>
        ... 
        <groups>WindowsTest</groups>
        <excludedGroups>SlowTest</excludedGroups>
        ...
    </execution>

但现在 slowWindowsTestCase方法永远不会执行。

编辑#2

即使我可以让测试执行正常工作,我也需要添加对 maven-surefire-plugin 的依赖项。使SlowTestWindowsTest可以在测试期间加载接口(interface)。我放了maven-surefire-pluginpluginManagement我的父 POM 的部分,但为此插件定义的任何依赖项都不能有 test范围。因此,在定义 SlowTest 的模块之外运行测试。和 WindowsTest接口(interface)将不起作用。我可以添加 test配置文件中插件的范围依赖关系,但这不会涵盖没有配置文件处于事件状态的情况。

最佳答案

您可以使默认配置文件排除所有组,并且对于每个配置文件,您添加一个故障安全/安全插件的执行,仅包括一个组。

关于unit-testing - 在 Maven 中组合配置文件以跳过多个类别的单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826068/

相关文章:

unit-testing - 如何仅在 Hybris 项目中运行单元测试?

java - Eclipse 4.7.0 中的 Tomcat 8.5 看不到已更改的 HTML 文件

java - Springboot 1.3.3 在 Jboss EAP 6.4.0GA 中给出 404

java - 手动安装的 Maven 依赖项在 Eclipse 导入中不可见

maven - 使用不同的 Maven 配置文件更改 Maven 依赖项的版本

c# - 具有上下文的模拟通用存储库

android - 如何使用 Mockito.mockStatic 在 kotlin android 中模拟静态方法

c++ - Catch lib(单元测试)和 CTest(CMake)集成

maven - 如何使用不同的配置文件管理artifactory/mavenartifact

java - 在eclipse tomcat上发布过滤后的web.xml