top of page

TestNG Data-Providers

What Are TestNG Data-Providers ?

It is an another way of parameterization in testNG that used to pass complex parameters.

It helps us to execute the same method multiple times by passing different data sets which means it allows users to write data-driven tests.

  • TestNG provides an annotaion '@DataProvider'  to implement this parameterization.This annotaion is declared with a method.

  • @DataProvider annotation contains an attribute 'dataProvider' which is its name in the test annotation.

  • TestNG dataprovider returns a 2-d list of objects.

  • The dataprovider name calls the dataprovider method and if name is not specified,then dataprovider method name is the default name used in test case.

  • @Test uses dataprovider name and its class name as attribute.

Data provider feature can be achieved in two ways :

By keeping dataprovider method and test cases in same class :

Syntax :

To declare the method : 

@DataProvider(name="Name of the Dataprovider")

public void Object[][] methodName(){

return new Object[][] {{values}};

}

 

To call the data provider in test :

@Test(dataProvider="Name of the Dataprovider")

 

Here is an example for dataprovider :

package testNGexamples;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class DataProvidersExample {
   WebDriver driver;


   @DataProvider(name="data-provider")
     public Object[][] dpMethod(){
         return new Object[][] {{"https://www.keynotesupport.com/internet/"},{"https://www.oxfordreference.com/page/scienceandtech/science-and-technology"}};
     }


  @Test(dataProvider="data-provider")
  public void dataProviderExample(String url) throws InterruptedException {
    driver.get(url);
    Thread.sleep(2500);
  }
 
  @BeforeMethod
  public void beforeMethod() {
     System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
     driver=new ChromeDriver();
     driver.manage().window().maximize();
  }

  @AfterMethod
  public void afterMethod() {
     driver.quit();
  }
 
}

By keeping dataprovider method and test cases in different classes(Inherited DataProvider) :

Syntax :

To declare the method : 

public class Class_Name{

@DataProvider(name="Name of the Dataprovider")

public void Object[][] methodName(){

return new Object[][] {{values}};

}

}

 

To call the data provider in test :

@Test(dataProvider="Name of the Dataprovider",dataProviderClass=  .class name of dataprovider class)

To implement inherited dataprovider we have to create two testNG classes : one for dataprovider and another for test cases.

So let's create a data provider class as 'DataProviderMethodClass' :

package testNGexamples;

import org.testng.annotations.DataProvider;

public class DataProviderMethodClass {
   @DataProvider(name="data-provider")
     public Object[][] dpMethod(){
       return new Object[][] {{"https://www.keynotesupport.com/internet/"},{"https://www.oxfordreference.com/page/scienceandtech/science-and-technology"}};
  }
}

 

Now create a testNG class as 'InheritedDataProvider' to inherit dataprovider :

 

package testNGexamples;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class InheritedDataProvider {
  WebDriver driver;


  @Test(dataProvider="data-provider",dataProviderClass=DataProviderMethodClass.class)
  public void inheritedDataProvider(String url) throws InterruptedException {
     driver.get(url);
     Thread.sleep(2500);
  }


  @BeforeMethod
  public void beforeMethod() {
     System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
     driver=new ChromeDriver();
     driver.manage().window().maximize();
  }

  @AfterMethod
  public void afterMethod() {
     driver.quit();
  }

}

 

 

How to pass multiple parameters in testNG dataprovider ?

 

We can pass multiple parameters in testNg by separating each parameter with comma ' , ' and can pass a cluster of multiple parameters by putting each dataset within parenthesis ' {} ' and separating each parenthesis ' {} ' with comma ' , ' .

Syntax :

@DataProvider(name="Name of the Dataprovider")

public void Object[][] methodName(){

return new Object[][] {{param00,param01,param02,param03},{param10,param11,param12,param13}};

}

Example :

package testNGexamples;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class InheritedDataProvider {
  WebDriver driver;


  @Test(dataProvider="data-provider",dataProviderClass=DataProviderMethodClass.class)
  public void inheritedDataProvider(String url) throws InterruptedException {
     driver.get(url);
     Thread.sleep(2500);
  }


  @BeforeMethod
  public void beforeMethod() {
     System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
     driver=new ChromeDriver();
     driver.manage().window().maximize();
  }

 

  @AfterMethod
  public void afterMethod() {
     driver.quit();
  }

}
 

I hope you will understand the concepts of dataprovider through this article.

Refer next page Include And Exclude InTestNG

bottom of page