![](/img/trans.png)
[英]How do I limit threads on a TestNG dataprovider method used as a factory
[英]How do I retrieve a specific row of data from a testng dataprovider?
我有一个像下面这样的数据提供者:
@DataProvider(name = "therapistToDoList") public static Object[][] data(){
return new Object[][]{
{"06012017", "Low", "This is a low task description added via automation for therapist/admin."},
{"06012017", "Medium", "This is medium task description added via automation for therapist/admin."},
{"06012017", "High", "This is high task description added via automation for therapist/admin."},
};
}
如何使用此数据提供程序运行测试用例,但仅针对特定的一行而不是全部?
使用testng dataprovider甚至可能吗?
如果使用该工具,则应遵循其遵循的意识形态。 数据提供者可以在那里为每个数据行运行测试。
如果您需要其他数据-请创建一个不同的DataProvider。 它可以调用最初的一个并从那里过滤值。 甚至更好-可能有一个较小的DataProvider,然后是一个较大的DataProvider,其中包含较小的值,并添加了自己的行。
一种方法可能是-您保留计数器并检查是否已达到目标数据,然后执行任务。
演示代码:
public class TestDemo {
public static int count = 0;
DataProvider(name = "therapistToDoList") public static Object[][] data(){
return new Object[][]{
{"06012017", "Low", "This is a low task description added via automation for therapist/admin."},
{"06012017", "Medium", "This is medium task description added via automation for therapist/admin."},
{"06012017", "High", "This is high task description added via automation for therapist/admin."},
};
}
@Test(dataProvider = "therapistToDoList")
public void testWithSpecificDataFromDataProvider(String a, String b, String c) {
count++;
if(count == 2){//let's say you are interest in 2nd data/row
System.out.println(a + " " + b + " " + c);
break;
}
}
}
这是一个工作示例,它通过JVM参数从TestNG套件xml文件(或)中接受特定的行作为选择,然后对数据提供者的行进行相应的过滤。
public class FilteredDataProviderExample {
@Test (dataProvider = "therapistToDoList")
public void testWithSpecificDataFromDataProvider(String a, String b, String c) {
Assert.assertNotNull(a);
Assert.assertNotNull(b);
Assert.assertNotNull(c);
}
@DataProvider (name = "therapistToDoList")
public static Object[][] data(ITestContext ctx) {
Object[][] data = new Object[][] {
{"06012017", "Low", "This is a low task description added via automation for therapist/admin."},
{"06012017", "Medium", "This is medium task description added via automation for therapist/admin."},
{"06012017", "High", "This is high task description added via automation for therapist/admin."},
};
return filterData(data, ctx.getCurrentXmlTest());
}
private static Object[][] filterData(Object[][] data, XmlTest xmlTest) {
//What if the value was given as a JVM argument.
int rowFromJVM = Integer.parseInt(System.getProperty("row", "-1"));
if (rowFromJVM > 0 && rowFromJVM < data.length) {
return new Object[][] {
data[rowFromJVM]
};
}
int row = - 1;
//Lets check if there was any filtering defined either at the <test> level or at the
//<suite> level.
if (xmlTest.getAllParameters().containsKey("row")) {
row = Integer.parseInt(xmlTest.getParameter("row"));
}
if (row > 0 && row < data.length) {
return new Object[][] {
data[row]
};
}
return data;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.