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

3

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?

Saxtuba answered 19/3, 2013 at 16:26 Comment(1)
Similar to this new question which focuses on having no extra configuration in IntelliJ/Eclipse to exclude the slow tests by default.Vinegarroon
U
2

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.

Upsurge answered 19/3, 2013 at 17:1 Comment(1)
What should I put in com.whatever.SlowTest? How does -DskipITs will know what to skip? I don't see any relation between the configuration in the XML and the "skipITs". Keep in mind that Maven is a mystery to me, I'm just trying to get some things done.Krispin
C
1

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.

Caye answered 19/3, 2013 at 16:30 Comment(2)
Thanks benzonico. This solves exactly what I asked and it's made me realise I probably need to edit the question because there's one extra case it does not catch. When I right click on src/test/java and select run as junit test, it still includes the slow performance tests in the other profiles that are not activated by default. Any idea how to fix this?Saxtuba
Thing is that last question is more related to eclipse configuration than maven.Caye
K
0

The native Junit case of your question can AFAIK only be kind of solved by keeping different tests in different source folders (or packages) and go to the run configuration at "Run all tests in ..." and limit the tests to run according to the specific source folder (or package).

I know of no versatile way to make that in a general way, but it's not too burdensome to make a new run configuration set per project (by duplicating an existing one) and remove those that are no longer relevant.

Kythera answered 14/1, 2014 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.