11

I'm currently working on a project which uses JUnit4 extensively throughout all the modules. We're using Maven2 to build the project and Hudson for continuous integration. The project is built and run under Java 1.5.

We've recently added quite a large raft of unit tests which are needed at times, but don't want to be run during the build process (but other unit tests in the same project do).

I want to know if there's some kind of annotation that can be applied to the tests, or some configuration that can be made to Maven that would ignore unit tests that live under a certain package or in a certain test class at build time?

There's possibly the alternative of just putting these particular tests in a normal class that would be run through main, but that's not ideal.

Thanks for your suggestions

James Camfield
  • 1,636
  • 3
  • 16
  • 24

8 Answers8

18

You can configure maven-surefire-plugin.

For example:

<project>
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <excludes>
        <exclude>**/TestCircle.java</exclude>
        <exclude>**/TestSquare.java</exclude>
      </excludes>
    </configuration>
  </plugin>
</plugins>

EDIT:
Another solution is to define profiles in which all test will be run e.g. by hudson. In development mode you can use subset of all test to speed up development (or all test might be in default profile, choosen test in dev profile - so you need run maven with -PprofileName attribute). This example better suits for integration tests that take loger to run.

cetnar
  • 9,357
  • 2
  • 38
  • 45
  • +1 for profiles (regarding integration tests, I'd rather place them in `src/test/it` and run them during the `integration-test` phase) – Pascal Thivent Oct 20 '09 at 09:51
  • Great, thanks for this I'll take a look into both solutions and decide which suits best. – James Camfield Oct 20 '09 at 10:11
  • @James. Notice that solution with profiles also need surefire plugin configuration. @Pascal. Yes, you are right, I do the same as you. But sometimes writing a *pure* unit test it's difficult, and always is a temptation to test a little more and still call it *unit test* :) – cetnar Oct 20 '09 at 10:22
7

There is an "Ignore" annotation, but it's manual work to add them, not sure if that helps.
It can be used for a test method or a whole class

@Ignore("not ready yet") @Test public void testSomething() {...

or

@Ignore public class SomeTest {
    @Test public void testSomething() {...
}

[]]

user85421
  • 28,957
  • 10
  • 64
  • 87
4

Try JUnit Assumptions, which seem to be since 4.4

Presuming you can detect at runtime if the tests should be run (via defines, properties, etc), this will allow you to keep a nice clean test suite.

MikeFHay
  • 8,562
  • 4
  • 31
  • 52
ptomli
  • 11,730
  • 4
  • 40
  • 68
3

I never tried it, but could you put the additional tests in a different source folder, and configure your build script to include or exclude it according to your build target?

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
1

'exclude' surefire plugin setting

denis.zhdanov
  • 3,734
  • 20
  • 25
1

Maven's Surefire Plugin: Inclusions and Exclusions of Tests

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
1

JUnit 4.7 I believe supports "Rules" for this kind of thing (pre 4.7 I think you could have used custom Runners who would check an environment variable).

Or you could look at the includes/excludes tags of your build system.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
-2

You need to add the following into your POM.xml

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.12.4</version>
   <configuration>
      <skipTests>true</skipTests>
   </configuration>
 </plugin>

Check out this URL : http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html

--Abhijit Das

abhijitcaps
  • 594
  • 8
  • 7