Monday, May 4, 2009

JUnit Parameterization

Now say you need to run the same test case for the multiple values. you can do this with the help of the parametrization.

JUnit 4 has changed the whole way you do the parametrization. it has introduced a special runner called Parameterized. It allows you to run the same test(s) with different datasets.

Now let's go step by step to make the test case parametrized ( data driven )
Use Parameterized Runner.
If you wanted to make any test case parameterized ( Data Driven ) you need to make it's whole class to run using Parameterized Runner.
This will be done with the help of the @RunWith annotation. Just add the following line before the class.

Make Dataset
Now you need to have dataset with all input values and expected output values. Here I have used two dimension array with one column as (input) value and other column as expected (output).
Now every testcase ( method with annotation @Test) will run as many times as the number of rows in the dataset (array).

Now let's move to the example
The example below will do the following task
1. Navigate to the Site.
2. For all the available dataset it will verify the factorial using google calculator.

import com.thoughtworks.selenium.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.*;

import java.util.regex.Pattern;
//require for the collection class
import java.util.*;

public class parameterization {

private static DefaultSelenium selenium;
private long expected;
private int value;

public static void init() /* throws Exception */ {
selenium = new DefaultSelenium("localhost", 4444, "*chrome","");

public static Collection data() {
return Arrays.asList( new Object[][] {
{ 1, 0 }, // expected, value
{ 1, 1 },
{ 2, 2 },
{ 24, 4 },
{ 5041, 7 },//wrong expected

public parameterization(long expected, int value) {
this.expected = expected;
this.value = value;
public void firstTest() throws Exception {"/");
selenium.type("q", value+"!=");"btnG");

String text = selenium.getText("//div[@id='res']/table[1]/tbody/tr[1]/td[3]/h2/b");
//to remove whitespaces
text= text.replace(" ","");
// will verify the text

If you will look at the above example you will find out that the init() method which is used to open the firefox browser and to navigate to site has marked annotation @BeforeClass rather then @Before.
This is because we need site to be opened for all the testcases.

The array which we have used is static. This is because we want it to be same for all the testcase. If it will not be static a new array will be created for the number of nows in the column. and so every test will get the same value, first row.

The another things is when you will run the last test case will show as fail. This is because we have put its expected value in the dataset as 5041 which is wrong. This is just to verify that the assert statement is working correctly.


dmitriy2 said...

I got this eror when i tried to use your script

java junit.textui.TestRunner parameterization
Could not create and run test suite: java.lang.ClassCastException: class paramet

Can you help me

Post a Comment