Question

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?

No correct solution

OTHER TIPS

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");
}
}

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
}

DataProvider ends up with kind of an annoying syntax when you do this. Here is an example:

@DataProvider(name="objectTestData")
public static Object[][] objectTestData() {
    return new Object[][] {
            { new TestData("hey", "you", "guys") },
            { new TestData("Sloth", "Baby", "Ruth") },
            { new TestData("foo", "bar", "baz") }
    };
}

@Test(dataProvider="objectTestData")
public void testScenario1(TestData data) {
    System.out.println(data.get(0) + "..." + data.get(1) + "..." + data.get(2));
}

static class TestData {
    public String[] items;

    public TestData(String... items) {
        this.items = items; // should probably make a defensive copy
    }

    public String get(int x) {
        return items[x];
    }
}

Alternatively, you can change the TestData constructor to put things into separate named methods. (For example if you were testing address data, there could be getName, getAddress, getCity.)

Use Map in @DataProvider to insert many parameters with the value, and return the object that contains the map, like below:

@DataProvider
public static Object[][]Dataprovider1(){
    Map<String, String> map = new HashMap<String, String>();
    map.put("data1", "value1");
    map.put("data2", "value2");
    ....
    map.put("data10", "value10");
    return new Object[][] {
        {map}
    };
}

In @test you can get the data by adding Map also as a sub parameter:

@Test(dataProvider = "Dataprovider1")
public void testScenario1(Map<String, String> data) {
    System.out.println(data.get("data1"));
    System.out.println(data.get("data2"));
    ....
}

It will produce:

value1
value2
....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top