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  an Object[][] array (two-dimensional), the size of the array represents the number of tests data and the number of variables used respectively.

If all the parameters are of same type(say String) then the return type can be changed to that data type (String[][]).

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 website 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”, data to these attributes are provided 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. In case this getData() method is created in some other class then another attribute called dataProviderClass = <full path of the class> can be passed.

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 or Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra:

Leave a Comment: