Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass > 10 parameters using TestNG DataProvider?

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?

like image 318
Naveen Kanak Avatar asked Oct 22 '25 04:10

Naveen Kanak


2 Answers

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");
}
}
like image 157
Bhushan Uniyal Avatar answered Oct 24 '25 19:10

Bhushan Uniyal


You can set the dataprovider to be an array of Object and use ArrayList> to have your parameters in key value pairs.

@DataProvider
public Object[][] getTestData()
{
    List<HashMap<String, String>> arrayMapList = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> hashMapItems = new HashMap<String, String>();

    //use a loop to fill in all the parameter name and value pairs
    hashMapItems.put("parameterName1", "parameterValue");
    hashMapItems.put("parameterName2", "parameterValue");
    //--------------More put statements here------
    //finally add hash map to the list
    arrayMapList.add(hashMapItems);

    //Iterate the array list and store each HashMap object in an object array. First dimension is the iterator value.
    Object [][] hashMapObj = new Object [arrayMapList.size()][1];

    for(int i=0; i<arrayMapList.size() ; i++) {
        hashMapObj[i][0] = arrayMapList(i);
    }

    return hashMapObj;
}

for each hashmap value in the array list, the test method will be run with its own set of parameters

@Test (dataProvider = "getTestData", enabled = true)
public void testDataRead(HashMap<String,String> hashMapValue)
{
    System.out.println(hashMapValue.get(parameterNameKey));  //parameter 1
    System.out.println(hashMapValue.get(parameterNameKey));  //parameter 2
}
like image 43
NethajiPrabhu Avatar answered Oct 24 '25 18:10

NethajiPrabhu