TestNG DataProvider
TestNG DataProvider is one of the most important features provided by TestNG. By using this feature user can write data-driven tests, that means a test method can run multiple times with different sets of data. In Parameterization using testng.xml file blog we have seen how to parameterize tests using testng.xml file. this feature is also one of the ways to pass the parameters to the test methods. It helps in providing the complex parameters to the test methods as this way is not possible with the testng.xml file.
To use the this feature in your tests you have to declare a method annotated by DataProvider(i.e. @DataProvider(name = “<data-provider name>”)) and then use the said method in the test method using the dataProvider attribute (i.e. @Test(dataProvider = “<data-provider name >”)) in the Test annotation.
1.DataProvider method and its using method are in Same class:
Below is the sample program:
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderInSameClass { @DataProvider(name = "data-provider") public Object[][] dataProviderMethod() { return new Object[][] { {"data-one"}, {"data-two"} }; } @Test(dataProvider = "data-provider") public void testMethod(String data) { System.out.println("Data Provided by Data Provider is: "+data); } }
The above test class contains a test method which takes one argument as input and prints it to console when executed. A DataProvider method is also available in the same class by using the DataProvider annotation of TestNG. The name of the said DataProvider method is mentioned using the name attribute of the DataProvider annotation. This returns a double Object class array with two sets of data, data-one and data-two.
The DataProvider is to provide parameter values to a test method is defined by giving the name of the data provider using the dataProvider attribute while using the Test annotation.
Execute the program using Right Click on the program and Run As → TestNG Test.
Below is the output of the program:
2.DataProvider method and its using method are in Different classes:
Now will see the example for DataProvider and its using methods are in different classes. Though we can use the DataProvider to get the parameters.
Below is the sample program using DataProvider to pass the parameters:
import org.testng.annotations.DataProvider; public class DataProviderClass { @DataProvider(name = "data-provider") public static Object[][] dataProviderMethod() { return new Object[][] { {"data-one"}, {"data-two"} }; } }
The preceding class only contains the DataProvider method to provide data to a test method. The method returns two sets of data when called.
Below is the sample program to read the parameters:
import org.testng.annotations.Test; public class DataProviderInOtherClass { @Test(dataProvider = "data-provider" , dataProviderClass = DataProviderClass.class) public void testMethod(String data) { System.out.println("Data Provided by Data Provider is: "+data); } }
The above class contains a test method which takes one argument as input and prints it onto the console when executed. The DataProvider to provide parameter values to a test method is defined by giving the name of the DataProvider using the DataProvider attribute while using Test annotation. As the DataProvider method is in a different class, the class name to refer for getting
the DataProvider is provided to TestNG using the dataProviderClass attribute as seen in the preceding code.
Execute the program using Right Click on the program and Run As → TestNG Test.
Below is the output of the program:
3.Multiple Parameters:
In the above example we have seen the data provider with only one parameter. In this will see how to pass multiple parameters to the test method.
Below is the sample program for multiple parameters:
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderWithMultipleParameters { @DataProvider(name = "data-provider") public Object[][] data() { Object[][] data = new Object[3][3]; data[0][0] = "FName1"; data[0][1] = "LName1"; data[0][2] = "Email1"; data[1][0] = "FName2"; data[1][1] = "LName2"; data[1][2] = "Email2"; data[2][0] = "FName3"; data[2][1] = "LName3"; data[2][2] = "Email3"; return data; } @Test(dataProvider="data-provider") public void readDataProviderData(String fName, String lName, String email) { System.out.println("First Name is: "+ fName); System.out.println("Last Name is: "+ lName); System.out.println("Email id is: "+ email); System.out.println("*********************"); } }
Execute the program using Right Click on the program and Run As → TestNG Test.
Below is the output of the program:
The above test class contains a test method which takes three arguments as input and prints it to console when executed. The DataProvider returns a double Object array with 3 parameters and 3 sets of data. In the object array first value indicates the sets of the data and second value indicated the parameters count. The above object array indicates 3 sets of data with 3 parameters.
The most important thing is the count of the object array second value must be same as the parameter count of the test method.
Conclusion:
1.Can pass the complex data to test methods as parameters using DataProvider.
2.Data Reading Class and Data Provider Class can be in the same class.
3.Data Reading Class and Data Provider Class can be in different classes.