DataProvider in TestNG

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 getData() method to data provider. We will send 3 rows and 2 columns ie. we will pass three different usernames and passwords.

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
	// "getdata" is the function name which is passing the data
       // Number of columns should match the number of input parameters
	public void setData(String username, String password)
		System.out.println("you have provided username as::"+username);
		System.out.println("you have provided password as::"+password);

	public Object[][] getData()
	//Rows - Number of times your test has to be repeated.
	//Columns - Number of parameters in test data.
	Object[][] data = new Object[3][2];

	// 1st row
	data[0][0] ="sampleuser1";
	data[0][1] = "abcdef";

	// 2nd row
	data[1][0] ="testuser2";
	data[1][1] = "zxcvb";
	// 3rd row
	data[2][0] ="guestuser3";
	data[2][1] = "pass123";

	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: 3, Failures: 0, Skips: 0

Output of the Above Program

you have provided username as::sampleuser1
you have provided password as::sampleuser1
you have provided username as::testuser2
you have provided password as::testuser2
you have provided username as::guestuser3
you have provided password as::guestuser3
PASSED: testdata("sampleuser1", "abcdef")
PASSED: testdata("testuser2", "zxcvb")
PASSED: testdata("guestuser3", "password123")
Test Frameworks: 


Good Example

Very helpful thanks for sharing

the dataProvider returns two dimensional array and if setData above passed as three arguments, then will that setData method works or will throw any exception.

Poi jar using read the Data From Excel in Data provider concept

I want to specify few commands before specific test that should apply for that method only,
can anybody help me out!!!


Can any one could let me know why my data provider is executing twice. (i.e. executing the same row twice)

Thank you very much. This is a great service you are doing for all learners. God bless you.

Good tutorial , Understandable to beginners , Keep Posting articles like this :)

Very nicely explained. Very helpful !

Thanks a lot !!

Add new comment