Joh*_*zen 8 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>
Run Code Online (Sandbox Code Playgroud)
因此,例如,当我想运行测试并排除任何慢速测试时,我可以执行mvn -PskipSlowTests test.如果我想跳过任何Windows测试,我可以mvn test在非Windows操作系统上执行(或-PskipWindowsTests在命令行中明确指定).
这里的问题是,当激活skipSlowTests和skipWindowsTests配置文件都被激活时,只会跳过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>
Run Code Online (Sandbox Code Playgroud)
这pom.xml是坏的,因为在为excludedGroups属性赋值时会有递归.
并且没有办法设置第三个配置文件(例如skipSlowTestsAndWindowsTests),无论何时激活skipSlowTests和skipWindowsTests激活.
这里有什么建议?可能会添加单独的阶段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>
Run Code Online (Sandbox Code Playgroud)
此配置将在三个单独的执行中运行测试:default-test执行将运行除慢速测试和Windows测试之外的每个测试; 在slow-test执行将只运行缓慢的测试; 并且windows-test执行只会运行Windows测试.如果启用了两个skipSlowTests和skipWindowsTests配置文件,则将跳过慢速测试和Windows测试.
这里的问题是当测试标记有两个SlowTest和WindowsTest类别时:
@Test @Category({SlowTest.class,WindowsTest.class})
public void slowWindowsTestCase() { ... }
Run Code Online (Sandbox Code Playgroud)
当skipSlowTests唯一的文件中启动,则windows-test执行仍然运行,并且由于slowWindowsTestCase方法被标记为窗口的测试,它将被执行,即使我们想跳过慢检验.同样,skipWindowsTests仅在启用配置文件时也将执行此测试用例.跳过此测试用例的唯一方法是同时指定skipSlowTests和skipWindowsTests配置文件.
我尝试修改执行如下:
<execution>
<id>slow-test</id>
...
<groups>SlowTest</groups>
<excludedGroups>WindowsTest</excludedGroups>
...
</execution>
<execution>
<id>windows-test</id>
...
<groups>WindowsTest</groups>
<excludedGroups>SlowTest</excludedGroups>
...
</execution>
Run Code Online (Sandbox Code Playgroud)
但现在该slowWindowsTestCase方法永远不会执行.
编辑#2
即使我可以使测试执行正常,我还需要添加一个依赖项,maven-surefire-plugin以便在测试期间加载SlowTest和WindowsTest接口.我已经放入了我的父POM maven-surefire-plugin的pluginManagement部分,但是为此插件定义的任何依赖项都没有test范围.因此,在定义SlowTest和WindowsTest接口的模块外部运行测试将不起作用.我可以test在配置文件中为插件添加范围依赖关系,但这不包括没有配置文件处于活动状态的情况.
| 归档时间: |
|
| 查看次数: |
6394 次 |
| 最近记录: |