Skip to content

Conditional Running of Tests

Marián Labuda edited this page Aug 12, 2015 · 1 revision

RunIf annotation and its usage

RedDeer allows you to modify flow of tests. If there are any tests depending on a condition you can tell whether those tests are supposed to run or not.

All you have to do is to annotate your test method with annotation @RunIf(conditionClass=MyCondition.class), where MyCondition class is an implementation of TestMethodShouldRun interface. RedDeer runner then evaluates whether a condition defined in a method public boolean shouldRun(FrameworkMethod method); is met or not. If the condition is met a test is executed. Otherwise a test is ignored. Remember, @Ignore annotation is superior to @RunIf annotation. That means even you would have a passing condition for a test marked with both annotation it would be still ignored because of presence of @Ignored annotation. For better understanding see following example:

package org.jboss.reddeer.junit.test.execution;

import org.jboss.reddeer.junit.execution.TestMethodShouldRun;
import org.junit.runners.model.FrameworkMethod;

public class ShouldNotRun implements TestMethodShouldRun {

	@Override
	public boolean shouldRun(FrameworkMethod method) {
		return false;
	}

	
}

and there is passing test to check how it really works:

package org.jboss.reddeer.junit.test.execution;

import static org.junit.Assert.fail;

import org.jboss.reddeer.junit.execution.annotation.RunIf;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(ExecutionTestRedDeerSuite.class)
public class TestMethodShouldRunTest {
	
	@Test
	public void testShouldRun1() {
		// PASSED
	}
	
	@Test
	@RunIf(conditionClass=ShouldNotRun.class)
	public void testShouldNotRun1() {
		fail("Test was not supposed to run because run if condition was not met.");
	}
	
	@Test
	@Ignore
	public void testShouldNotRun2() {
		fail("Test was not supposed to run because @Ignore annotation is presented.");
	}
	
	@Test
	@RunIf(conditionClass=ShouldNotRun.class)
	@Ignore
	public void testShouldNotRun4() {
		fail("Test was not supposed to run because @Ignore annotation is presented and run condition was not met.");
	}
Clone this wiki locally