7

In my code I have 2 types of tests: fast unit tests and slow performance tests. I am trying to ensure that the performance tests are never run unless the user explicitly wants to run them (e.g. using the right test suite or maven build profile) because this kicks of a remote process that is computationally-intensive on a grid.

I understand how to create build profiles that exclude (or include) specific tests using maven-surefire-plugin as explained on this site. The problem I have, however, is that I have not found a way to exclude tests from the default run while still providing the option to run the test when needed. By default run I mean:

  • Right click on the project in eclipse and selects "run as| Maven test"
  • Right click on src/test/java and select run as junit test

In both cases above it runs all the unit tests (include the slow tests I hope to exclude by default). The same thing happens if there is a typo in the build profile I try to run.

Is there a way I can excluse performance tests from the default run without having to move them to a separate maven module?

4

3 回答 3

2

我们在项目中所做的就是用一个特殊的标记接口来标记慢速单元测试。 @Cathegory(SlowTest.class)注释并将它们添加到故障安全插件中。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-failsafe-plugin</artifactId>
   <configuration>
       <groups>com.whatever.SlowTest</groups>
   </configuration>
    <executions>
      <execution>
        <configuration>
            <includes>
                <include>**/*.class</include>
            </includes>
        </configuration>
        <goals>
            <goal>integration-test</goal>
            </goals>
    </execution>
</executions>

如果您不想执行它们:使用 maven 选项 -DskipITs。这将跳过它们。

于 2013-03-19T17:01:08.740 回答
1

默认情况下,使用profiles您可以使一个活动不包括您的慢速测试,并使用另一个来运行所有测试。

您必须在您的fastTestOnly个人资料中指定这些标签:

<profiles>
  <profile>
    <id>fastTestOnly</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

这样,当您运行简单时,mvn install只有选择的配置文件是fastTestOnly.

于 2013-03-19T16:30:33.077 回答
0

您问题的本机 Junit 案例只能通过将不同的测试保存在不同的源文件夹(或包)中来解决 AFAIK,然后转到“在...中运行所有测试”的运行配置并限制测试运行特定的源文件夹(或包)。

我知道没有通用的方法可以以通用的方式实现这一点,但是为每个项目创建一个新的运行配置集(通过复制现有的配置集)并删除那些不再相关的配置并不会太麻烦。

于 2014-01-14T19:48:14.120 回答