DataProvider in TestNG
In the previous post, we have learnt about parameters annotation in TestNG.In this post , we will learn another way to perform parameterization in TestNG.
Apart from Parameters, there is another way to achieve parameterization which is by using DataProvider in TestNG.
DataProviders are used for data driven testing which means same test case can be run with different set of data. It is very powerful feature of TestNG and effectively used during framework development. There are few points you should know about dataProvider.
- It marks a methods for supplying the data to other methods.
- Annotated methods return an array of Object i.e. Object[][].
- DataProvider can have a name, and it will be used in other methods by using its name.
- DataProvider can be implemented in the same class or different class.
- A Data Provider is a method annotated with @DataProvider.
We will understand the functionality of dataProvider with 2 examples.
DataProvider used in the Same Class: DataProvider can be implemented in the same class where the function using the dataProvider is residing. In the example, a class(ExampleDataProvider) is having a @Test method (TestLogin) as well as @DataProvider method(getData).
The @DataProvider method(getData) is returning an object array containing email ids and passwords.
The @Test method(TestLogin) is receiving data from dataProvider by using its name (LoginDataProvider).
Code:
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ExampleDataProvider { @Test(dataProvider="LoginDataProvider") public void TestLogin(String email,String password){ System.out.println(email +"--"+password); } @DataProvider(name="LoginDataProvider") public Object[][] getData(){ Object[][] data = new Object[2][2]; data[0][0] = "shekhar@Gmail.com"; data[0][1] = "abc@123"; data[1][0] = "1234@Gmail.com"; data[1][1] = "xyz@123"; return data; } }
Output:
As you can observe in the output that TestLogin method has been executed 2 times with different set of data passed from DataProvider.
DataProvider used in the different class: Now, we will see if we keep method annotated with @DataProvider in another class, then how to use it. Suppose,we have 2 classes named as “ExampleDataProvider” where @Test method is present and “CustomDataProvider” where @DataProvider is present.
We will use the same @DataProvider as used in previous example, but put it in different class.
Class 1:
import org.testng.annotations.DataProvider; public class CustomDataProvider { @DataProvider(name="LoginDataProvider") public static Object[][] getData(){ Object[][] data = new Object[2][2]; data[0][0] = "shekhar@Gmail.com"; data[0][1] = "abc@123"; data[1][0] = "1234@Gmail.com"; data[1][1] = "xyz@123"; return data; } }
Class 2:
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ExampleDataProvider { @Test(dataProvider="LoginDataProvider",dataProviderClass= CustomDataProvider.class) public void TestLogin(String email,String password){ System.out.println(email +"--"+password); } }
Output: TestLogin will be executed with 2 set of data.