Skip to toolbar

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.

DataSource.java

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;
	}
}

GenerateReport.java

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:

JIRA:

Agile:


Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 4
  • Page views today : 6
  • Total visitors : 49,368
  • Total page view: 67,965

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.