Skip to content

Parameterized tests

Radim Hopp edited this page Jul 14, 2015 · 6 revisions

Since version , RedDeer is capable of running parameterized tests (similar to JUnit parameterized tests).

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

package test.pkg;

<imports>

@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.

package test.pkg;

<imports>

@RunWith(RedDeerSuite.class)
@SuiteClasses({MyParameterizedTestClass.class, MySimpleTestClass.class})
public class MyTestSuite {
}
package test.pkg;

<imports>

public class MySimpleTestClass {

	@Test
	public void SimpleTestClassTest1(){
		System.out.println("SimpleTest1");
	}
	
	@Test
	public void SimpleTestClassTest2(){
		System.out.println("SimpleTest2");
	}
}
package test.pkg;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

<imports>

@UseParametersRunnerFactory(ParameterizedRequirementsRunnerFactory.class)
@JBossServer(type = ServerReqType.ANY, state = ServerReqState.RUNNING)
public class MyParameterizedTestClass {
	
	@InjectRequirement
	ServerRequirement 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(req.getConfig()));
	}

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

}

And the result is:

Clone this wiki locally