I think a "good way" to solve this should fulfill the following:
- a dynamically ignore test-case should marked as "ignored" in the JUnit test-run,
- possible search for dynamically ignored test-cases in the IDE.
There is a very simple way to ignore a test-case base on run-time information:@Test
public void testIt() {
if (shouldIgnore())
return;
// ... the rest of the test-case.
}
However, this solution does not fulfill the above requirements at all. Something better is needed.
JUnit uses a org.junit.runner.Runner to run test-cases. Since JUnit 4.0 it's possible to define which such Runner that should be used to run a set of test-cases. The @RunWith annotation does just this. Here is an example:
@RunWith(MyRunner.class)
public final class MyTest {
// ... some test-cases.
}
There are several ways @RunWith can make you testing filled days easier; for a real-world example you need to look no further than to JMock. I have implemented a Runner that makes it possible to do:@RunWith(RuntimeIgnoreable.class)
public final class MyTest {
@Test
public void perhapsIgnored() {
ignoreIf(perhapsTrue())
// ... the rest of the test-case.
}
}
Neat, ey? I think so at least. What's even neater is that the RuntimeIgnoreable class and the ignoreIf method was embaressingly straight-forward to implement. You can browse the code, or look at the individuals files:
Oh, one final note: this was develop for JUnit 4.3. If you are using any other JUnit version, the you prbably need to make some minor changes to the code.
Update: I just realize that JUnit Extensions does this (among aother things)...