Preface
I always believed that using @Ignore to deactivate tests is a bad idea. Except, maybe as one way to put tests that fail intermittently into quarantine to attend to them later (as Martin Fowler describes it here). This bears the danger that the test suite decays as more and more tests keep getting ignored and forgotten. Therefore you should have a policy in place to ensure that tests aren’t quarantined for too long. Well, so I thought until recently when we ran into this:
In a project that Frank and I work on, we ran into an SWT issue described here. On non-Windows platforms, asserting whether an SWT widget has got the input focus does not work with automated tests. We decided to ignore focus-related tests on non-Windows platforms for now. Though our build server runs on Linux we found it safe enough as both our development environments run on Windows.
In JUnit, assumptions are the means to skip tests that aren’t meaningful under the given condition. Expressed that way, our test would look like this:
- public void testFocus() {
- assumeTrue( isRunningOnWindows() );
- // ...
- }
- SomeTest.java (Test Case)
- package test;
- import static org.junit.Assert.*;
- import org.junit.Rule;
- import org.junit.Test;
- import test.ConditionalIgnoreRule.ConditionalIgnore;
- public class SomeTest {
- @Rule
- public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();
- @Test
- @ConditionalIgnore( condition = NotRunningOnWindows.class )
- public void testNotInWindows() {
- System.out.printf("\t[Info] Running testing...\n");
- assertTrue(!System.getProperty( "os.name" ).startsWith( "Windows" ));
- }
- @Test
- @ConditionalIgnore( condition = RunningOnWindowsOnly.class )
- public void testOnlyInWindows() {
- System.out.printf("\t[Info] Running testing...\n");
- assertTrue(System.getProperty( "os.name" ).startsWith( "Windows" ));
- }
- }
- package test;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- import java.lang.reflect.Modifier;
- import org.junit.Assume;
- import org.junit.rules.MethodRule;
- import org.junit.runners.model.FrameworkMethod;
- import org.junit.runners.model.Statement;
- public class ConditionalIgnoreRule implements MethodRule {
- public interface IgnoreCondition {
- boolean isSatisfied();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ ElementType.METHOD })
- public @interface ConditionalIgnore {
- Classextends IgnoreCondition> condition();
- package test;
- import test.ConditionalIgnoreRule.IgnoreCondition;
- public class NotRunningOnWindows implements IgnoreCondition{
- @Override
- public boolean isSatisfied() {
- String os = System.getProperty( "os.name" );
- System.out.printf("\t[Debug] OS=%s (Skip test case? %s)\n", os, os.startsWith("Win"));
- return os.startsWith("Win");
- }
- }
- package test;
- import test.ConditionalIgnoreRule.IgnoreCondition;
- public class RunningOnWindowsOnly implements IgnoreCondition{
- @Override
- public boolean isSatisfied() {
- String os = System.getProperty( "os.name" );
- System.out.printf("\t[Debug] OS=%s (Skip test case? %s)\n", os, !os.startsWith("Win"));
- return !os.startsWith("Win");
- }
- }
The ConditionalIgnore annotation requires a ‘condition’ property that points to a class that implements IgnoreContition. At runtime, an instance of theIgnoreContition implementation is created and its isSatisfied() method decides whether the test is ignored (returns true) or not (returns false). Finally there is anIgnoreConditionRule that hooks the annotations into the JUnit runtime.
If the IgnoreCondition implementation decides to ignore a test case, an AssumptionViolatedException is thrown. Therefore the ConditionalIgnore annotation has the same effect as if an Assume condition would return false. With a slight difference that we consider an advantage: @Before and @After methods are not executed for ignored tests.
The source code of the rule and its related classes can be found here.
The remaining issue with Assume is that it affects the test statistics. If an Assume condition is found to be false, it treats the test as having passed even though it hasn’t run. To overcome that, you’d have to provide your own runner that handles AssumptionViolatedException the way you want.
Even though I just wrote about ignoring tests in length, I am still convinced that tests at best should not be ignored and if so only in exceptional cases.
Supplement
* Stackoverflow - Conditionally ignoring tests in JUnit 4
沒有留言:
張貼留言