In TestNG, a data provider is a method that provides test data for a test method. The test method can then use the data provided by the data provider to run multiple test cases with different input values.
Here's an example of how to use a data provider in TestNG:
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class MyTest { @DataProvider(name = "testData") public Object[][] testData() { return new Object[][] { {1, 2, 3}, {4, 5, 9}, {-1, -2, -3} }; } @Test(dataProvider = "testData") public void testAdd(int a, int b, int expected) { int result = a + b; assertEquals(result, expected); } }
In this example, the testData
method is a data provider that returns an array of test data. The testAdd
method is a test method that takes three arguments: a
, b
, and expected
. The testAdd
method is annotated with the @Test
annotation and the dataProvider
attribute is set to the name of the data provider, "testData".
When the test is run, TestNG will invoke the testAdd
method for each row of test data returned by the testData
data provider. In this case, the testAdd
method will be called three times with the following arguments:
a
= 1, b
= 2, expected
= 3a
= 4, b
= 5, expected
= 9a
= -1, b
= -2, expected
= -3You can find more information about data providers and how to use them in TestNG in the TestNG documentation.