Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude specific unit tests from default "maven test" and eclipse "run as junit test"

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?

like image 820
Kes115 Avatar asked Mar 19 '13 16:03

Kes115


2 Answers

What we have done in our projects is to mark the slow unit tests with a special marker interface. @Cathegory(SlowTest.class) annotation and add them to the failsafe plugin.

<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>

If you don't want to execute them: use the maven option -DskipITs. This will skip them.

like image 198
T K Avatar answered Sep 23 '22 10:09

T K


Using profiles you can make one active by default that excludes your slow tests and use another one to run all tests.

You have to specify those tags in your fastTestOnly profile :

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

This way, when you run a simple mvn install only the profile picked is fastTestOnly.

like image 31
benzonico Avatar answered Sep 21 '22 10:09

benzonico