@DataProvider
假设您将数据放在属性文件中,以下是如何使用 a 执行此操作:
public class A {
@Test(dataProvider = "dp")
public void test(String k, String v) {
System.out.println("Testing " + k + " " + v);
Assert.assertEquals(k.toUpperCase(), v);
}
@DataProvider
public Object[][] dp() throws FileNotFoundException, IOException {
Properties p = new Properties();
p.load(new FileInputStream(new File("/tmp/a.properties")));
List<Object[]> result = Lists.newArrayList();
for (Map.Entry<Object, Object> es : p.entrySet()) {
result.add(new Object[] { es.getKey(), es.getValue() });
}
return result.toArray(new Object[result.size()][]);
}
@Test(dataProvider = "dp")
public void test(String k, String v) {
System.out.println("Testing " + k + " " + v);
Assert.assertEquals(k.toUpperCase(), v);
}
}
属性文件:
abc: ABC
def: DEF
ghi: GHI
和输出:
Testing abc ABC
Testing def DEF
Testing ghi GHI
PASSED: test("abc", "ABC")
PASSED: test("def", "DEF")
PASSED: test("ghi", "GHI")
===============================================
Test1
Tests run: 3, Failures: 0, Skips: 0
===============================================
请注意,每组参数都会传递给测试方法(因此测试方法被调用了 3 次),并且该测试方法将这些参数声明为常规方法参数。
有关数据提供者的更多详细信息:http: //testng.org/doc/documentation-main.html#parameters-dataproviders