Skip to content

Parameterized tests

rawagner edited this page Aug 8, 2017 · 6 revisions

Usage is more or less same as usage of pure JUnit parameterized tests (as RedDeer builds on top of that) with few differences:

  • There is no need to use @RunWith(Parameterized.class). For tests class to be recognized as parameterized, the class has to contain method annotated @Parameters.
  • If user want's to use requirements & other RedDeer specific functionality, he has to annotate that class with @UseParametersRunnerFactory(ParameterizedRequirementsRunnerFactory.class).

Parameters could be passed using constructor, or injected into class using anontation @Parameter as it is in pure JUnit parameterized tests.

Simple example

The simplest example would be

import java.util.Arrays;
import java.util.Collection;

import org.eclipse.reddeer.junit.internal.runner.ParameterizedRequirementsRunnerFactory;
import org.eclipse.reddeer.junit.runner.RedDeerSuite;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

@RunWith(RedDeerSuite.class)
@UseParametersRunnerFactory(ParameterizedRequirementsRunnerFactory.class)
public class SimplestParameterizedTest {
	
	@Parameters
	public static Collection<Object> data(){
		return Arrays.asList(new Object[] {1,2,3,4,5});
	}
	
	private int fInput;
	
	public SimplestParameterizedTest(int input) {
		fInput = input;
	}
	
	@Test
	public void test(){
		System.out.println(fInput);
	}

}

This would result into all test methods from class SimplestParameterizedTest to be run five times (once for each argument). Exact result of such test will look like:

Advanced example with test suite and ServerRequirements

In this example we will have one top test suite class, which will contain two test classes (one is parameterized, one isn't). We will see how parameterized tests can be named and how parameterized tests work together with requirements.

import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;

import org.eclipse.reddeer.junit.runner.RedDeerSuite;

@RunWith(RedDeerSuite.class)
@SuiteClasses({MyParameterizedTestClass.class, MySimpleTestClass.class})
public class MyTestSuite {

}
import org.junit.Test;

public class MySimpleTestClass {

	@Test
	public void simpleTestClassTest1(){
		System.out.println("MySimpleTestClassTest1");
	}
	
	@Test
	public void simpleTestClassTest2(){
		System.out.println("ySimpleTestClassTest1");
	}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.reddeer.junit.internal.runner.ParameterizedRequirementsRunnerFactory;
import org.eclipse.reddeer.junit.requirement.inject.InjectRequirement;
import org.eclipse.reddeer.requirements.server.ServerRequirementState;
import org.eclipse.reddeer.requirements.server.apache.tomcat.ApacheTomcatServerRequirement;
import org.eclipse.reddeer.requirements.server.apache.tomcat.ApacheTomcatServerRequirement.ApacheTomcatServer;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

@UseParametersRunnerFactory(ParameterizedRequirementsRunnerFactory.class)
@ApacheTomcatServer(state = ServerRequirementState.RUNNING)
public class MyParameterizedTestClass {
	
	@InjectRequirement
	ApacheTomcatServerRequirement req;

	@Parameters(name="{index}: {0}")
	public static Collection<String> data() {
		List<String> arrayList = new ArrayList<String>();
		arrayList.add("Test1");
		arrayList.add("Test2");
		arrayList.add("Test3");
		return arrayList;
	}
	private String argument;
	
	public MyParameterizedTestClass(String argument) {
		this.argument = argument;
	}

	@Test
	public void parameterizedTest1() {
		System.out.println("ParameterizedTest1 "+argument +" "+ req.getServerNameLabelText());
	}

	@Test
	public void parameterizedTest2() {
		System.out.println("ParameterizedTest2 "+argument+" "+ req.getServerNameLabelText());
	}

}

And the result is:

Clone this wiki locally