Software Testing Learning Hub

Dataprovider in TestNG

Dataprovider is of the most efficient way to make your script datadriven by provide the datasets. Dataprovider is an TestNG annotation which facilitate automation tester to define the data in the method or extract data from external files such as Excel sheet.

in this post we will see how to use DataProvider in your script.

DataProvider using data inside the method:

package SmokeTestSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
public class Dataprovider {
 @Test(dataProvider = "dp")
 public void f(Integer n, String s) {
 
 System.out.println("In:"+ n +" and s "+ s );
 }
 @BeforeMethod
 public void beforeMethod() {
 }
 @DataProvider
 public Object[][] dp() {
 return new Object[][] {
 new Object[] { 1, "a" },
 new Object[] { 2, "b" },
 };
 }
}

 

outcome of the program:

n:1 and s a
n:2 and s b
PASSED: f(1, "a")
PASSED: f(2, "b")

 

i will post code for Dataprovider using external data source like Excel soon.. stay tune.