@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.
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]
--
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.
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.