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
ramya
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?

AyubHussain
Posts: 138
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 {
@Test(dataProvider="empSalary")
public void processSalary(String empName, int empSal) {
System.out.println("Employee: " + empName);
System.out.println("Salary: " + empSal);
}
@DataProvider
public Object[][] empSalary() {
return new Object[][]{{"John", 15000}, {"Andi", 18000}};
}
}

Post Reply

Return to “TestNG Testing Framework for Selenium”