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.
@RunWith(value=Parameterized.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
Example
The example below will do the following task
1. Navigate to the Google.co.in Site.
2. For all the available dataset it will verify the factorial using google calculator.
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 google.co.in site has marked annotation @BeforeClass rather then @Before.
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.*;
@RunWith(value=Parameterized.class)
public class parameterization {
private static DefaultSelenium selenium;
private long expected;
private int value;
@BeforeClass
public static void init() /* throws Exception */ {
selenium = new DefaultSelenium("localhost", 4444, "*chrome","http://google.com");
selenium.start();
}
@Parameters
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;
}
@Test
public void firstTest() throws Exception {
selenium.open("/");
selenium.type("q", value+"!=");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
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
assertEquals(value+"!="+expected,text);
}
}
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.
1 comments:
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
erization
Can you help me
Post a Comment