TestNG – 14 || DataProvider annotation & attribute.

DataProvider is the most important annotation while using TestNG framework. It helps to execute same Test Case on multiple Test Data.

@DataProvider annotation defines the data source in the same class file or separate class file. While dataProvider attribute provides access to that data source in any Test Method defined under @Test annotation.

DataProvider Annotation and attribute.

DataProvider Sample Example

To understand the above process we have taken two classes. The first class is “DataSource.java” using DataPovider annotation.


There are a few points we need to take care while using this annotation and defining method under this.

  1. Use @DataProvider annotation.
  2. name attribute used to declare the name of the DataProvider. If not declared we can use method name directly for dataProvider attribute.
  3. The method should be declared static if we are declaring @DataProvider in a separate class.
  4. The return type of this method should be Object[row][col]. It is a two-dimension array of Object.
    • row: Shows how many records it contains and how many times a test method will execute.
    • col: How many arguments present in one row and how many Argument values passed for each iteration while executing the Test Method.
package dataProviderExample;
import org.testng.annotations.DataProvider;

 * @author ashok.kumar
public class DataSource {

	@DataProvider(name = "database")
	public static Object[][] getData() 
		String[][] userCredentials = { 
				{ "cred001", "pwd@1234" }, 
				{ "sandy89", "pwd#987" }, 
				{ "krish00", "pwd$678" }
		return userCredentials;


We can access data stored/define in the above file in any Test Methods.

  1. Use @Test annotation.
  2. dataProvider attribute used to access the data source. It would be the same value declare with name attribute in the previous class file.
  3. dataProviderClass attribute gives the path of the class file where data source present. It would be the filename of that class. In this example DataSource.class
  4. As Object[][] contains two values in each row. Hence the method declared under @Test annotation should have two arguments of String type to receive values from the data source.

Note: Normally the format of the return type would be Object[row][col]. So, a test method will repeat itself depending upon the value of the row from the object. In this example, the Test Method will execute 3 times. We can analyze the same in the Result section after execution.

package dataProviderExample;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

 * @author ashok.kumar
public class GenerateReport {
	@Test(dataProvider="database",dataProviderClass = DataSource.class)
	public void report(String userName,String password)
		System.out.println("User Name: "+userName+" Password: "+password);

Result Analysis

The output of the Console. Here we can observe Total Tests run show 3 which is the total number of records we have stored in the DataSource file using @DataProvider annotation.

Result in index.html file.

Related Links:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:



1285total visits,1visits today

Leave a Reply
