DataProvider Annotation In TestNG

Install TestNG Testing Framework on Eclipse IDE, TestNG Annotations, Create TestNG Test Cases, Prioritize TestNG Test Cases, Group TestNG Test Cases, Data Driven Testing, Parallel Testing and Generate Test Reports.
Post Reply
Posts: 89
Joined: Fri Nov 23, 2018 2:55 am

DataProvider Annotation In TestNG

Post by ramya » Wed Nov 28, 2018 5:51 am

What Is DataProvider Annotation In TestNG And Why You Use It?

Posts: 142
Joined: Fri Nov 23, 2018 2:48 am

Re: DataProvider Annotation In TestNG

Post by AyubHussain » Wed Nov 28, 2018 5:52 am

The TestNG framework introduced the data provider annotation to support data-driven testing. It allows us to use both internal and external sources of input.

If the input size is small, then we can keep the data inside the project files. However, in most cases, the need is to have a big dataset to feed into the test cases. And no tester or a developer would like to bloat his/her code with such a massive set of input.

Hence, the solution is to use the TestNG “@DataProvider” annotation. It can allow a test to accept input from external data sources like CSV, MS Excel, and many others.

Please find below a simple example of using the “@DataProvider” annotation to read input from a 2-D array.

public class DataDrivenTest {
public void processSalary(String empName, int empSal) {
System.out.println("Employee: " + empName);
System.out.println("Salary: " + empSal);
public Object[][] empSalary() {
return new Object[][]{{"John", 15000}, {"Andi", 18000}};

Posts: 85
Joined: Thu Jan 03, 2019 3:58 am

Re: DataProvider Annotation In TestNG

Post by srini » Fri Jan 04, 2019 6:31 am

*It converts any method into data supplier method
*A broad range of test data are parameterized through @DataProvider annotation
*Test data from other sources like excel sheet, JSON, etc are injected through @DataProvider
*It returns the 2-dimensional array of object
*@DataProvider sets value for the @Factory annotation

*Its default value is its method name. If you forgot to use (name=”dataProvider”) then you can proceed by using the method name inside @DataProvider annotation
*When we have a large number of values in @DataProvider then it takes all values as default. But we can restrict it by defining its indices as @DataProvider(name=”dataprovider”, indices={1, 5, 7, 9})
*TestNG is loosely coupled test framework, we can connect Test class and @DataProvder class together by using property dataProviderClass as @Test(dataProvider =”dataprovider”, dataProviderClass= DataProviderClass.class)

Post Reply

Return to “TestNG Testing Framework for Selenium”