Search code examples
testngtestng-dataprovider

How Data Provider of TestNG sends data to Test internally?


My question is :

How "Data Provider" sends the data to the Test internally ?

What actually happens behind the scenes, so that we get the sent data in the "Test"?.

I am very curious to know that, how data provider and test get connected to each other just by mentioning name of the "data provider", and "data Provider" sends data to Test.

Does "Data Provider" calls the test and passes the data as parameters ?

testMethod("data one", "Data two");

My Data Provider code :

   import org.testng.annotations.DataProvider;

    public class DataProviderClass {

    @DataProvider(name = "data-provider")
        public static Object[][] dataProviderMethod() {
        return new Object[][] { { "data one" }, { "data two" } };
    }
}

My Test code :

@Test(dataProvider = "data-provider")
public void testMethod(String dataOne, String dataTwo) {
    System.out.println("Data is: " + dataOne + " " + dataTwo);
}

Solution

  • The sources are here: https://github.com/cbeust/testng

    But in few words, TestNG calls the data provider by introspection and stores its values in memory. Then, TestNG iterates on the 2dim array and calls the test method with values of each 1dim array.

    Easy, isn't it?