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 the name attribute in the previous class file.
  3. dataProviderClass attribute gives the path of the class file where the 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 the same datatype it was declared in the data source to receive values from the data source. In this example, both the arguments should be of String type.

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.

Example with Multiple DataSource and DataType

Here we are taking a Class DataSource to store the data in String format, Int format, and Mixed format. And another Class AccessData to read the data.

DataSource.class

import org.testng.annotations.DataProvider;

/**
 * @author ashok.kumar
 */
public class DataSource {
	
	@DataProvider(name="database")
	public static Object[][] getData()
	{
		String[][] info= {
				{"user1","pwd1"},
				{"user2","pwd2"},
				{"user3","pwd3"}
		};
		return info;
	}
	
	@DataProvider
	public static Object[][] getInfo()
	{
		Object[][] info= {
				{1,2},
				{3,4}
		};
		return info;
	}
	
	@DataProvider
	public static Object[][] getMix()
	{
		Object[][] info= {
				{1,"Computer",1990},
				{3,"Laptop",2020}
		};
		return info;
	}
}

AccessData.class

import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class AccessData {
	
	@Test(dataProviderClass = ClassA.class, dataProvider = "database")
	public void viewData(String a,String b)
	{
		System.out.println("Password for user "+a+" is "+b);

	}
	
	@Test(dataProviderClass = ClassA.class, dataProvider = "getInfo")
	public void viewDataNum(int a,int b)
	{
		System.out.println("Value of Key "+a+" is "+b);
	}
	
	@Test(dataProviderClass = ClassA.class, dataProvider = "getMix")
	public void viewDataMix(int a,String b,int c)
	{
		System.out.println("Serial number "+a+" Product is "+b+". Year ="+c);
	}
}

Result Analysis

Result in eclipse console.

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 : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

   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.