I wanted to know if there is a way to package all parameters to a @Test
method into one class Name
and provide Name[]
as the @DataProvider
?
public class FirstTestClass {
@Test (dataProvider = "getNames")
public void test01(Name name) {
System.out.println(name.name + " " + name.id);
}
@DataProvider
public Name[] getNames() {
Name[] result = new Name[2];
result[0] = new Name("john", 5);
result[1] = new Name("doe", 4);
return result;
}
}
class Name {
public String name;
public Integer id;
public Name(String name, Integer id) {
this.name = name;
this.id = id;
}
}
The test is getting skipped and I get a message saying must return either Object[][] or Iterator<>[]
. This is kind of limited right? Any workaround for this?
Why are you not able to do the same thing using two dimensional array with only 1 column. Is there any specific requirement that you have? Something like this
@Test(dataProvider = "getNames")
public void test01(Name name) {
System.out.println(name.name + " " + name.id);
}
@DataProvider
public Object[][] getNames() {
Name[][] result = new Name[2][1];
result[0][0] = new Name("john", 5);
result[1][0] = new Name("doe", 4);
return result;
}
I tested your code with the change I am suggesting it works just fine.
Change it to
@DataProvider
public Name[][] getNames() {
Name[][] result = new Name[2][];
result[0] = {new Name("john", 5)};
result[1] = {new Name("doe", 4)};
return result;
}
I don't know why you use a dedicated class to hold all attributes, but you should consider something like:
public class FirstTestClass {
@Test(dataProvider = "getNames")
public void test01(String name, Integer id) {
System.out.println(name + " " + id);
}
@DataProvider
public Object[][] getNames() {
return new Object[][] {
new Object[] { "john", 5 },
new Object[] { "doe", 4 }
}
}
}
In JUnit 4.x, the following is the way to achieve the above scenario.
@RunWith(Parameterized.class)
public class FirstTest {
private LoginCredentials loginData;
public FirstTest(LoginCredentials testData) {
loginData = testData;
}
@Parameterized.Parameters
public static LoginCredentials[] generateTestData() {
return ReadingExcelSheets.generateTestData();
}
I was hoping for something similar in TestNG, which I was told is more flexible and powerful than JUnit. Turns out, we have return only Object[][] in the @DataProvider method.
As I understand your question now, you'd like a TestNG translation of:
@RunWith(Parameterized.class)
public class FirstTest {
private LoginCredentials loginData;
public FirstTest(LoginCredentials testData) {
loginData = testData;
}
@Parameterized.Parameters
public static LoginCredentials[] generateTestData() {
return ReadingExcelSheets.generateTestData();
}
}
A solution could be:
public class FirstTest {
private final LoginCredentials loginData;
@Factory(dataprovider = "generateTestData")
public FirstTest(LoginCredentials testData) {
loginData = testData;
}
@Test
public void test01() {
System.out.println(loginData);
}
@DataProvider
public static Object[][] generateTestData() {
return convert(ReadingExcelSheets.generateTestData());
}
public static Object[][] convert(LoginCredentials[] values) {
Object[][] result = new Object[values.length];
for (int i=0; i<result.length; i++) {
result[i] = new Object[]{ values[i] };
}
return result;
}
}
Or
public class FirstTest {
@Test(dataprovider = "generateTestData")
public void test01(LoginCredentials loginData) {
System.out.println(loginData);
}
@DataProvider
public static Object[][] generateTestData() {
return convert(ReadingExcelSheets.generateTestData());
}
public static Object[][] convert(LoginCredentials[] values) {
Object[][] result = new Object[values.length];
for (int i=0; i<result.length; i++) {
result[i] = new Object[]{ values[i] };
}
return result;
}
}
But ok, TestNG could provide the convert
method itself and/or manage Object[]
as a valid return type out-of-the-box.
Feel free to propose a patch on http://github.com/cbeust/testng Help is always welcome :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.