TestNG – Using @DataProvider and @Factory

TestNG gives you two awesome annotations to implement Data Driven tests. Lets look at them one by one:-

  1. @DataProvider – This annotation can be used to denote that the method will be used for supplying data to a test method. The condition that needs to be met here is that the method marked as @DataProvider must return a 2D Object array (Object[][]) where each Object[] will be used as the input parameter to an iteration of the test method which uses the data provider. Please note that there is no direct way of linking an Excel sheet or CSV to a Data Provider. In the following example, I will be using Apache POI to read an excel to provide the data to my Test Method:-

public Object[][] GetSearchData() {
//Returning 2D array of Test Data
Object[][] arrayObject = readFromExcel("C:/InputData.xls","Sheet1");
return arrayObject;

public String[][] readFromExcel(String fileName, String sheetName) {
String[][] arrayExcelData = null;
try {

//Read the data from the Excel Sheet
FileInputStream fs = new FileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet(sheetName);

int totalNoOfCols = sh.getColumns();
int totalNoOfRows = sh.getRows();
arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

for (int i= 1 ; i < totalNoOfRows; i++) {

for (int j=0; j < totalNoOfCols; j++) {
arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();

} catch (Exception e) {
return arrayExcelData;

//Using the Data Provider in the Test Method
@Test(dataProvider = "SearchData")
public void SearchData(String searchTerm)
//use searchTerm in the test case. The Test method will run as many times as the
//rows in the excel(with searchTerm having different value everytime)

2. @Factory – This is another way of achieving data driven testing using testng.
This feature allows all methods in a class to be run on multiple sets of data. Contrary
to the @DataProvider annotation, this can be used to instantiate a class multiple times
rather than just a method. The factory method should return an Object[]. This can be an
array of Method calls or class objects. We can run the Factory class directly. Here’s a
sample code that shows this:-

public class FactoryTest
private string searchValue;

public FactoryTest(string searchValue) {
this.searchValue= searchValue;

public void testMethodOne() {
 //do something with searchValue

public void testMethodTwo() {
 //do something else with searchValue

public class FactoryClass
public Object[] factoryMethod() {
//Return an array of Objects of Factory Test Class, each with a different search value
return new Object[] { new FactoryTest("SearchTerm1"), new FactoryTest("SearchTerm1") };

In a nutshell, @DataProvider gives you the power to run a test method with different sets of data and @Factory gives you the power to run all methods inside a test class with different sets of data. Though you can also run test methods with @Factory also, it depends on your use case as to which approach fits it better. Please comment in case the difference between the two is not clear 🙂

Happy Automating!
Harshit Kohli


Eat, Sleep, Automate. REPEAT !

Tagged with: , , , , , ,
Posted in Awesum Stuff

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 168,130 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: