DataProvider in TestNG

August 19, 2016

Let us consider a scenario in which a test case (@Test) needs multiple test data, DataProvider (@DataProvider) is an annotation which can be used to provide multiple test data to a test case. The return type of DataProvider is Object[][] array (two-dimensional), the size of the array represents the number of tests data and the number of variables used respectively.

For Example – Object[5][2] – It represents a test data with 5 pairs of values.

Let us understand DataProvider with an example:

Let’s consider a scenario we want to test login of facebook site for 4 different credentials.

Code to login into facebook site with 5 pairs of credentials provided through test data:

Here, in the above code, a test case is written which accepts two inputs “username” and “password”, which we are providing through one of the DataProvider, “getData” below. The return type of getData() method is Object[][], a 2-Dimensional array of size 4 * 2, where 4 is the number of test data and 2 is the number of variable. An attribute dataprovider=”getData” is passed in the test case.

Once we execute this class. facebookLogin Method will be executed 4 times with 1 set data (username and password) in each run.

We can have multiple DataProviders in a class file for different methods.

PS: For any questions queries or comment feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning 🙂

Saurabh

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: