4

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?

Barett
  • 5,826
  • 6
  • 51
  • 55
Naveen Kanak
  • 73
  • 2
  • 2
  • 6
  • If your method takes 10 parameters, you need to declare it with 10 parameters. – Cedric Beust Apr 30 '13 at 16:35
  • @CedricBeust if we declare more than 10 parameters in the test method then code was looking some what odd, i wanted to know is there any way to declare these number of parameters dynamically in the parameter list section of the test method. – Naveen Kanak May 02 '13 at 04:31
  • Pass them inside an object then. Either way, it's a Java question, not a TestNG one. – Cedric Beust May 02 '13 at 18:19

4 Answers4

6

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");
}
}
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
2

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
}
  • @mkl what is searchStrings.get(i) ? – Benjamin McFerren May 18 '18 at 01:20
  • @BenjaminMcFerren *"what is searchStrings.get(i)"* - I have no idea. The answer is by NethajiPrabhu, I merely improved the formatting a bit. – mkl May 18 '18 at 08:02
  • @BenjaminMcFerren Considering the whole answer though... I assume that code has been copied&pasted together from something bigger, probably `searchStrings` should have been `arrayMapList`, at least this would match the signature of the actual test method. – mkl May 18 '18 at 08:17
  • @BenjaminMcFerren mkl is correct, my bad it should have been arrayMapList. – NethajiPrabhu Sep 09 '19 at 03:31
  • The line `hashMapObj[i][0] = arrayMapList(i);` should be `hashMapObj[i][0] = arrayMapList.get(i);` – Ayush Choudhary Jul 19 '22 at 06:17
0

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.)

Barett
  • 5,826
  • 6
  • 51
  • 55
0

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
....
frianH
  • 7,295
  • 6
  • 20
  • 45