Pages

Wednesday, 14 December 2011

Data-Driven testing using Junit and TestNG

Most of the guys who are into automation may be knowing the term Data-Driven testing. But the word will still be new for some fresh faces in the field of automation. In this blog I will explain what is Data-Driven testing and will give an example of Data-driven testing using Junit and TestNG frameworks.

Data-Driven testing as the name suggests is a test driven by the Data. For ex. You have a user detail form where you need to enter details of multiple users and save them. Here if you have 5 different user data available and you have to write automation cases for these, you may end up writing 5 different automation test cases(one for each user data).
If you apply a Data-Driven approach you will end up with only one test-case, that is filling the form with user data and then submitting it. The test case will get executed based on the data provided to it. In this case it will be 5 and hence the test case will get executed 5 times with different data-set.

The advantage of using a Data-driven approach is that you reduce your effort in writing/maintaing test-cases for your different type of data. In case of additions or deletion of new/old entries , you just have to change the data and not your actual test-case.

Following I will mention a Data-Driven approach for searching on google with different data using Junit and TestNg frameworks:

Using Junit:

import static org.junit.Assert.fail;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.Arrays;
import java.util.List;

@RunWith(Parameterized.class)
public class JunitGoogleBase {
public Selenium selenium;
WebDriver driver;
private String testData;

public JunitGoogleBase(String testData){
this.testData=testData;
}

@Parameters
public static List< Object[]> data() {
return Arrays.asList(new Object[][]{{"testing"},{"Software testing"}});
}

@Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver, "http://www.google.com");
selenium.open("http://www.google.com");
}

@Test
public void testSearch() throws Exception {
selenium.open("/");
selenium.type("id=lst-ib", testData);
selenium.click("//input[@value='Google Search']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

}

@After
public void tearDown() throws Exception {
selenium.stop();

}
}

Using TestNg:

 import com.thoughtworks.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGGoogleBase {
public Selenium selenium;
WebDriver driver;

@DataProvider(name="parameter")
public static Object[][] data() {
return new Object[][]{{"testing"},{"Software testing"}};
}

@BeforeMethod
public void setUp() throws Exception {
driver= new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver, "http://www.google.com");
selenium.open("http://www.google.com");
}

@Test(dataProvider="parameter")
public void testSearch(String testData) throws Exception {
selenium.open("/");
selenium.type("id=lst-ib", testData);
selenium.click("//input[@value='Google Search']");
for (int second = 0;; second++) {
if (second >= 60) Assert.fail("timeout");
try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
for (int second = 0;; second++) {
if (second >= 60) Assert.fail("timeout");
try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
}

@AfterMethod
public void tearDown() throws Exception {
selenium.stop();
}

}
<span class="Apple-style-span" style="font-family: Verdana,sans-serif;">
</span>

The main difference in the above two functions is that you provide a Paramaterized option to the class in Junit and supply data to the constructor of the said class. Where as in TestNG you do the same at the test-method level.

Its simple to do data-driven testing in TestNG framework as you can provide a different data providing function for each test-method, but the same is not possible in Junit.

No comments:

Post a Comment