2016-09-05 76 views
0

我想知道是否有办法将所有参数打包为@Test方法为一个类Name并提供Name[]作为@DataProviderTestNG中的@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; 
    } 
} 

该测试正在跳过,我收到一条消息说must return either Object[][] or Iterator<>[]。这是有限的权利?任何解决方法?

回答

0

将其更改为

@DataProvider 
public Name[][] getNames() { 
    Name[][] result = new Name[2][]; 
    result[0] = {new Name("john", 5)}; 
    result[1] = {new Name("doe", 4)}; 
    return result; 
} 
+0

在我看来,这是非常有限的...... JUnit有参数化方法接受自定义类数组作为数据提供者...可以说我想测试从DB自动填充表单...表返回的行将有这样很多领域......方法签名不能包含所有的变量吗? TestNG在这方面非常有限...... –

+0

@ChandrachudNanduri如果你喜欢'JUnit',你应该使用它。但是,在这里你有相同的自定义类只包裹在数组中,我不认为这是什么大不了的事。 – talex

0

我不知道为什么你使用一个专用的类来保存所有属性,但你应该考虑是这样的:

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 } 
    } 
    } 
} 
+0

在我看来,这是非常有限的... JUnit有接受自定义类数组作为数据提供者的参数化方法...可以说我想测试从DB自动填充表单...返回的行将有很多字段...方法签名不能包含所有的变量吗? TestNG在这方面非常有限...... –

+0

我不理解你的观点。你能分享你的junit方式吗? – juherr

+0

'@RunWith(参数化。类) public class FirstTest { \t private LoginCredentials loginData; \t \t public FirstTest(LoginCredentials testData){ \t \t loginData = testData; \t} \t @ Parameterized.Parameters \t公共静态LoginCredentials [] generateTestData(){ \t \t返回ReadingExcelSheets.generateTestData(); \t}' @Julien –

0

在JUnit 4.x中,以下是实现上述场景的方法。

@RunWith(Parameterized.class) 
public class FirstTest { 
    private LoginCredentials loginData; 

    public FirstTest(LoginCredentials testData) { 
     loginData = testData; 
    } 

@Parameterized.Parameters 
public static LoginCredentials[] generateTestData() { 
    return ReadingExcelSheets.generateTestData(); 
} 

我希望在TestNG中有类似的东西,我被告知比JUnit更加灵活和强大。原来,我们只在@DataProvider方法中返回Object [] []。

0

正如我现在明白你的问题,你想的TestNG的翻译:

@RunWith(Parameterized.class) 
public class FirstTest { 
    private LoginCredentials loginData; 

    public FirstTest(LoginCredentials testData) { 
     loginData = testData; 
    } 

    @Parameterized.Parameters 
    public static LoginCredentials[] generateTestData() { 
     return ReadingExcelSheets.generateTestData(); 
    } 
} 

一个解决办法是:

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

或者

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

而行, TestNG可以提供convert方法本身和/或管理Object[]作为有效的re即开即用型。

随意提出对http://github.com/cbeust/testng 帮助补丁总是欢迎:)

+0

我刚刚开了一个相关的问题的项目:https://github.com/cbeust/testng/issues/1139 – juherr

0

你为什么不能做使用二维数组一样的东西,只有1列。你有什么具体要求吗?像这样的东西

@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; 
} 

我测试了你的代码和更改我建议它工作得很好。

+0

okie ......这是一种解决方法,但它不是理想解决方案...我觉得如果JUnit 4.x支持Object [],那么TestNG应该是先进的和更灵活的框架:) –