How To Use TestNg Data Provider

TestNg (Testing framework for the next generation) is developed specifically for Java programming Language. It is similar to Junit but offers additional functionalities like additional annotations, parallel execution support etc. In this post, we will delve into the TestNg data provider and its usage in detail.

What is Parameterization?

Parameterization is the important concept of data-driven testing which refers to passing dynamic inputs to methods or scripts to test the same logic against multiple data sets and configurations. In Test automation, while performing cross-browser execution we need to pass different configurations at run time, this is one of many examples of parameterization.

How to Perform Parameterization Using TestNG

We use below mentioned syntax in TestNG Java for parameterization.

@Parameters({"username", "password"})

What is TestNg Data Provider?

DataProvider is a TestNg Annotation used to pass the parameters in Test functions. Data providers are widely 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 objects i.e. object[][].

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

DataProvider Syntax

The TestNG data provider is the inbuilt function and returns a 2D list of objects that follows the mentioned syntax.

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

Prerequisite:

1. Java installation and configuration.

2. IDE

3. Maven project with Selenium Jars.

4. TestNg Dependency.

How to Use DataProvider In TestNg

Using a data provider is very easy. It can be utilized in multiple ways as per the framework’s requirements. Let’s see how many ways we can utilize TestNG data providers in our test automation framework.

1) Use TestNG data provider Within the Same Class

This is the most straightforward 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. The steps are as follows:

1. Create a new test class inside the maven project and name it “DemoDataProvider”.

2. Define a method inside the “DemoDataProvider” class that uses the @DataPovider annotation and returns a 2D object array.

3. Create a test method with the @Test notation that maps to the previous method and prints out the data.

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);
  }
}

2) Inherited DataProvider in TestNG

data provider and Test method can also be written in separate classes. This approach is very useful for maintaining 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 data provider class.

Sample DataProvider Class

  public class DataProviders {
	
	@DataProvider(name="LoginTestDataProvider")
	public Object[][] getInvalidCredentials()
	{
	return new Object[][] {
	{"[email protected]","admin123"},
	{"[email protected]","abc123"}
	};
	}
	
	@DataProvider(name="EmailDataProvider")
	public Object[][] getEmail()
	{
	return new Object[][] {
	{"[email protected]"},
	};
	}
	}

In the above code, a utility class is created which contains multiple data providers. This class can be inherited in the test class mentioned below.

Test Class

Create a test class and inside the @Test annotation pass the utility class name and the required data provider name for the test function.

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

What is a partial TestNG data provider?

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 achieve this requirement? This can be achieved using a partial data provider.

How can we partially pass the data to the method using the data provider?

We use indices to pass the partial data. Indices are nothing but indexes of which data we need to pass.

What are indices in the Data provider?

Indices are the one parameter inside the data provider annotation that accepts the integer array. We pass the index value of the data set that 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, we can use the concept of the partial data provider and execute only the failing data set. For that, we need to use indices and provide the index value of our intended data set.

   @DataProvider(indices = {0,3})
   
	 public String[][] dp1()
	 {
   String [][]data = new String[][]
	 {
	 {"[email protected]","admin123"},
	 {"[email protected]","abc123"},
	 {"[email protected]","abc123"},
	 {"[email protected]","abc123"},
	 };
	 return data;
	 }

In the above code snippet, we have a data provider that supplies data for 4 users. I need to pass the details of only the first and fourth users in my test method. To achieve this I have provided the index {0, 3} with the @dataprovider annotation.

While executing the test script now data provider will provide data only for the first and fourth users and the rest will be ignored.

TestNg Data Providers execution result

How To Run Data Provider in Parallel (Not The Test Cases)

Suppose we want to execute the data provider in parallel mode to feed the test data to test methods parallelly rather than sequentially. Use attribute (parallel=true) else it will execute in sequential order by default.

How to maintain 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 need to control how many sets of data can be executed parallelly. This can be handled using the data provider thread count mechanism.

<?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> 

Created an XML file and added the attribute “data-provider-thread-count” at the suite level with the desired value. 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 divided and fed as 3 data sets per browser.

To read more about testNg you can visit the official documentation.

Conclusion

TestNg data providers are very useful in maintaining the reusability and scalability of test scripts. It also helps in maintaining a thread-safe environment along with parameterization and data-driven testing. To learn more about test automation-related concepts please visit here.

2 thoughts on “How To Use TestNg Data Provider”

  1. This is very valuable and useful information for automation testers. Thank you for providing this.

Comments are closed.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading