views:

767

answers:

2

OK, so the @Ignore annotation is good for marking that a test case shouldn't be run.

However, sometimes I want to ignore a test based on runtime information. An example might be if I have a concurrency test that needs to be run on a machine with a certain number of cores. If this test were run on a uniprocessor machine, I don't think it would be correct to just pass the test (since it hasn't been run), and it certainly wouldn't be right to fail the test and break the build.

So I want to be able to ignore tests at runtime, as this seems like the right outcome (since the test framework will allow the build to pass but record that the tests weren't run). I'm fairly sure that the annotation won't give me this flexibility, and suspect that I'll need to manually create the test suite for the class in question. However, the documentation doesn't mention anything about this and looking through the API it's also not clear how this would be done programmatically (i.e. how do I programatically create an instance of Test or similar that is equivalent to that created by the @Ignore annotation?).

If anyone has done something similar in the past, or has a bright idea of how else I could go about this, I'd be happy to hear about it.

+4  A: 

You should checkout Junit-ext project. They have RunIf annotation that performs conditional tests, like:

@Test
@RunIf(DatabaseIsConnected.class)
public void calculateTotalSalary() {
    //your code there
}

class DatabaseIsConnected implements Checker {
   public boolean satisify() {
        return Database.connect() != null
   }
}

[Code sample taken from their tutorial]

notnoop
Thanks for this answer - an interesting alternative syntax for the functionality, though I'll be going with `Assume` directly so as not to introduce another dependency.
Andrzej Doyle
I personally prefer this solution. If you have many tests that should be run based on the same conditions, this would be far more ideal than having to use Assume in every test. Also, if this can be used on a class level rather than the method level, it will be even more ideal.
Richard
+8  A: 

The JUnit way is to do this at runtime is org.junit.Assume.

 @Before
 public void beforeMethod() {
     org.junit.Assume.assumeTrue(someCondition());
     // rest of setup.
 }

You can do it in a @Before method or in the test itself, but not in an @After method. If you do it in the test itself, your @Before method will get run.

An assumption failure causes the test to be ignored.

Edit: To compare with the @RunIf annotation from junit-ext, their sample code would look like this:

@Test
public void calculateTotalSalary() {
     assumeThat(Database.connect(), is(notNull()));
    //test code below.
 }

Not to mention that it is much easier to capture and and use the connection from the Database.connect() method this way.

Yishai
@notnoop, that isn't my observation at all. They are ignored. The IDEA test runner reports them that way, and a look at the JUnit source code shows that it reports the test as ignored.
Yishai
To quote: "In the future, this may change, and a failed assumption may lead to the test being ignored." It in fact changed, as of 4.5 I believe. The current javadoc says: "The default JUnit runner treats tests with failing assumptions as ignored. Custom runners may behave differently." http://github.com/KentBeck/junit/blob/7aac4b19d359285041ccb51d575235339a1a8be0/src/main/java/org/junit/Assume.java
Yishai
Thanks! I didn't catch that in the 4.5 release notes! Thanks!
notnoop
Thanks, that's exactly what I'm looking for. The semantics of `Assume` sound like exactly what I'm trying to do here, so even if a runner doesn't treat it as `@Ignore` I trust it will be handled appropriately.
Andrzej Doyle