Dataprovider and varargs

93 views
Skip to first unread message

sesh...@gmail.com

unread,
Jun 22, 2016, 4:34:59 PM6/22/16
to testng-users
I was trying to use dataprovider with varargs to my test.
I found below code when i googled it and that works well 


@DataProvider(name = "testData")

    public static Object[][] testDataProvider() {

        return new Object[][] {new String[]{"abc","abcd"},new String[]{"abc","abcd","123"}};

    }


    @Test(dataProvider = "testData")

    public void test(String... str) {

        System.out.println("\tThe Length is"+str.length);


    }


But ...my requirement is that my test case uses first two parameters and a variable length argument.

When i change above code to suit my needs below by adding two variable and varargs fro my test case:


@DataProvider(name = "testData")

    public static Object[][] testDataProvider() {

        return new Object[][] {new String[]{"abc","abcd"},new String[]{"abc","abcd","123"}};

    }


    @Test(dataProvider = "testData")

    public void test(String param1, String param2, String... str) {

        System.out.println("\tparam1 is"+param1+"\t param2 is: "+param2);

        for (String param : str)

            System.out.println("\t Other params are: : "+param);    


    }


It does not get executed, i get exception;  argument type mismatch


Suggest.



⇜Krishnan Mahadevan⇝

unread,
Jun 23, 2016, 12:02:58 AM6/23/16
to testng...@googlegroups.com
Maybe you can try wrapping your Vargs into your own class and then work with it.

Please see below for a sample :

public class VargsExample {

@Test (dataProvider = "dp")
public void testMethod(String arg1, String arg2, Vargs vargs) {
System.err.println("Mandatory Arg1 " + arg1);
System.err.println("Mandatory Arg2 " + arg2);
System.err.println("Variable Arguments " + Arrays.toString(vargs.getArgs()));
}

@DataProvider (name = "dp")
public Object[][] getData() {
return new Object[][] {
{"Selenium", "TestNG", new Vargs("Java", "Python")},
{"Selenium", "TestNG", new Vargs("Hindi", "Tamil", "Telugu")},
{"Selenium", "TestNG", new Vargs("Math", "Physics", "Chemistry", "Biology")}
};
}

public static class Vargs {
private String[] args;

public Vargs(String... args) {
this.args = args;
}

public String[] getArgs() {
return args;
}
}
}

Output :
Mandatory Arg1 Selenium
Mandatory Arg2 TestNG
Variable Arguments [Java, Python]
Mandatory Arg1 Selenium
Mandatory Arg2 TestNG
Variable Arguments [Hindi, Tamil, Telugu]
Mandatory Arg1 Selenium
Mandatory Arg2 TestNG
Variable Arguments [Math, Physics, Chemistry, Biology]



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Julien Herr

unread,
Jun 29, 2016, 11:30:37 AM6/29/16
to testng-users

⇜Krishnan Mahadevan⇝

unread,
Jun 29, 2016, 11:26:36 PM6/29/16
to testng...@googlegroups.com
Julien,
No it doesn't work when a user mixes a set of fixed arguments and vargs.
The test that you shared makes use of ONLY vargs.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Julien Herr

unread,
Jun 30, 2016, 3:54:26 AM6/30/16
to testng...@googlegroups.com
Yes, true ! Will have to replace the varargs by an array (which is the same things in fact) and adapt the data provider.

public void test(String param1, String param2, String[] str) { ...

return new Object[][] { new Object[]{"abc","abcd", new String[]{}}, new Object[]{"abc","abcd", new String[]{"123"}};




You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/c9FK6c5-y1o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages