Skip to content

How To Use TestNg Data Provider

What is TestNg Data Provider?

DataProvider is a TestNg Annotation used to pass the parameters in Test functions. Data providers are used for data-driven testing which means the same test can be executed with a different set of data. It is a very powerful feature of TestNG. There are a few important points to note while using a data provider:

1)TestNg DataProvider name is always string type in nature.

2)It marks a method for supplying the data to another method.

3)Annotated method returns an array of object i.e object[][].

4)Data providers can be used in the same class or in a different class.

DataProvider Syntax

@DataProvider (name = "name_of_dataprovider")
public Object[][] methodName()
 {
   return new Object [][] { values}
}

How to Use DataProvider In TestNg

The data provider can be utilized in multiple ways as per the framework’s need. Let’s see them one by one in action.

Within Same Class

This is the most straightforward and simplest way to use a data provider. We have to declare a method annotated by @DataProvider and then use this method in the tests using the ‘dataProvider‘ attribute in the @Test annotation.

public class DemoDataProvider {

  @DataProvider(name = "dataproviderdemo")
  public Object[][] dataProviderMethod() {
    return new Object[][]{{"data one"}, {"data two"}};
  }

  @Test(dataProvider = "dataproviderdemo")
  public void testMethod(String data) {
    System.out.println("Data is: " + data);
  }
}

Inherited DataProvider

DataProvider and Test method can be written in separate classes. This approach is very useful to maintain code reusability and readability. Suppose we have multiple data providers for different test methods then we can create a utility class that can keep all the data providers and this class can be used by using the attribute dataProviderClass.

Sample class for DataProvider

      public class DataProviders {
	
	@DataProvider(name="LoginTestDataProvider")
	public Object[][] getInvalidCredentials()
	{
	return new Object[][] {
	{"Admin@gmail.com","admin123"},
	{"test@gmail.com","abc123"}
	};
	}
	
	@DataProvider(name="EmailDataProvider")
	public Object[][] getEmail()
	{
	return new Object[][] {
	{"Admin@gmail.com"},
	};
	}
	}

Sample class for Test methods

public class HomePageTest
{
@Test(dataProvider="LoginTestDataProvider",dataProviderClass = DataProviders.class)
public void SignInWithInvalidUser(String email,String password) 
  {
     home.clickSignIn();
     signin.enterEmail(email)
     .enterPassword(password)
     .clickSubmit();
   } 
 }

How to use TestNg Data Providers partially

The data provider can have multiple sets of data and out of all those multiple sets of data we want to use only a few sets of data, How can we do that? this can be achieved using a partial data provider but how can we partially pass the data to the method using the data provider?

We use indices for that. Indices are nothing but indexes of which data we need to actually pass.

What are indices

Indices are the one parameter inside data provider annotation which accepts the integer array. We pass the index value of the data set which we want to use partially for our data provider. If we don’t use the indices then the entire data provided data will be supplied to the test method.

Where can we use it (in which scenario)?

Practically our data provider can have huge data sets let’s say we have 100 sets of data and 4-5 data set tests are failing. It is not an ideal approach to execute all 100 test cases again for debugging, instead of that we can use the concept of the partial data provider and execute only the failing data set. For that we need to use attribute indices and provide the index value of our intended data set.

         @DataProvider(indices = {0,3})
	 public String[][] dp1()
	 {
         String [][]data = new String[][]
	 {
	 {"Admin@gmail.com","admin123"},
	 {"test1@gmail.com","abc123"},
	 {"test2@gmail.com","abc123"},
	 {"test3@gmail.com","abc123"},
	 };
	 return data;
	 }

Now run the above test. Only 2 test cases were executed out of a total of four test cases based on the provided indices.

How To Run data provider in parallel(Not The Test Cases)

If we want to run our data provider in parallel mode then we need to use (parallel=true)else it will execute in sequential order by default. if it is true that means data is fed to the test method parallelly.

@DataProvider(parallel=true)

Thread count in data provider

Suppose we have 100 sets of Test data and we set parallel=true then 100 browsers will open, which may cause issues related to the network or any other execution-related issues so we should be able to control how many sets of data can be executed parallelly. This can be handled using the data provider thread count mechanism.

Create an XML file and add attribute data-provider-thread-count in the suite tag with the value. So this will control how many browsers will open at a time and what set of data will feed to those browsers. For example, if we define data-provider-thread-count=“3” and we have 9 data sets then data will be fed as 3 data sets per browser.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="2">
  <test name="Test">
    <classes>
      <class name="com.github.AHQ.tests.DataproviderDemo"/>
    </classes>
  </test> 
</suite> 

@DataProvider is a very useful concept in TestNg. We can even merge data providers and make our script more reusable and readable.