- Nov 01, 2018
- admin
- 0
DataProvider in TestNG
DataProvider marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method.
The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
The name of this data provider. If it’s not supplied, the name of this data provider will automatically be set to the name of the method.
In the below example we will pass the data from getStudData() method to data provider. We will send 2 rows and 2 columns.
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderExample{
//This test method declares that its data should be supplied by the Data Provider
// "getStuddata" is the function name which is passing the data
// Number of columns should match the number of input parameters
@Test(dataProvider="getStudData")
public void setStudData(String studName, String studCity)
{
System.out.println("you have provided stud name as::"+studName);
System.out.println("you have provided stud city as::"+studCity);
}
@DataProvider
public Object[][] getStudData()
{
//Rows - Number of times your test has to be run / repeated.
//Columns - Number of parameters in test data.
Object[][] data = new Object[2][2];
// 1st row
data[0][0] ="Mike";
data[0][1] = "Baltimore";
// 2nd row
data[1][0] ="David";
data[1][1] = "Newyork";
return data;
}
}
When we execute the above example, we will the get the output as below:
Each data set that we pass will be considered as a test method. As we passed three set of data to the data provider, it will display result as below:
Default suite
Total tests run: 2, Failures: 0, Skips: 0
Output of the Above Program
you have provided stud name as
::Mike you have provided stud city as::Baltimore you have provided stud name as
::David you have provided stud city as::Newyork PASSED: testdata("Mike", "Baltimore") PASSED: testdata("David", "Newyork")