views:

421

answers:

7

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

+3  A: 

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
Interesting, not a bad idea. If nothing else comes up, I'll look into it. Cheers
James Camfield
+1  A: 

'exclude' surefire plugin setting

denis.zhdanov
+11  A: 

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
+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
Great, thanks for this I'll take a look into both solutions and decide which suits best.
James Camfield
@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
A: 

Maven's Surefire Plugin: Inclusions and Exclusions of Tests

sfussenegger
Damn, I didn't notice that I open this question a while ago :)
sfussenegger
+1  A: 

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.

mlk
+3  A: 

Try JUnit Assumptions, which seem to be since 4.4

http://junit.sourceforge.net/doc/ReleaseNotes4.4.html
http://junit.org/apidocs/org/junit/Assume.html
http://eclipsesource.com/blogs/2009/10/07/using-junits-assume-for-faster-tests/

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.

ptomli
+2  A: 

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() {...
}

[]]

Carlos Heuberger