how to pass > 10 parameters using TestNG DataProvider?

Naveen Kanak picture Naveen Kanak · Apr 30, 2013 · Viewed 10.5k times · Source

I need to pass more than 10 parameters to a TestNG Dataprovider, and the code look some what like this ...

@Test (dataProvider = "Dataprovider1")
public void testScenario1(String data1, String data2,
                          String data3, String data4,
                          String data5 //...
            ) throws Exception {
    System.out.println(data1+"---------------- "+data2+" ---------------   "+data3+" .. so on");
}

Can anyone tell me what approach we should follow in case we need to pass more than 10 parameters using @DataProvider? Is there any other way to declare the parameters for the test method?

Answer

Bhushan Uniyal picture Bhushan Uniyal · May 9, 2017

If you have same type of parameters then you can pass as a array in method parameter.

@Test (dataProvider = "Dataprovider1")
public void testScenario1(String args [])
            ) throws Exception {
    System.out.println(args[0]+"---------------- "+args[1]+" ---------------   "+args[3]+" .. so on");
}

Also if you have different type of parameter field then you can beak it with help of a helper class and then pass the reference of this class in parameter. e.g:

class Helper {
  String data1;
  String data2;
  String data3;
  Long data4;
  int data5;
  flot data6;
 -----so on------
 ----getter setter and constructor----
}

your test class

class Test {
@DataProvider(name="Dataprovider1")
public static Object[][] testData() {
    return new Object[][] {
            { new Helper("hey", "you", "guys" ..... another constructor parameters..) } }
    };

}

@Test (dataProvider = "Dataprovider1")
public void testScenario1(Helper helper) throws Exception {
    System.out.println(helper.data1+"---------------- "+helper.data2+" ---------------   "+helper.data3+" .. so on");
}
}