What is the use of the @DataProvider annotation in TestNG?
A) To provide data to a test method
B) To provide setup information for a test suite
C) To clean up resources after a test
D) To specify the order of test execution
Answer:
A) To provide data to a test method
Explanation:
The @DataProvider
annotation in TestNG is used to provide data to a test method. It allows you to run the same test multiple times with different sets of data. A method annotated with @DataProvider
returns an array of objects, which are passed as parameters to the test method.
For example:
@DataProvider(name = "testData")
public Object[][] createData() {
return new Object[][] {
{ "data1" },
{ "data2" }
};
}
@Test(dataProvider = "testData")
public void testMethod(String data) {
// Test code using the provided data
}
In this example, the testMethod()
will be executed twice, once with “data1” and once with “data2”.