Java 如何从testng数据提供程序检索特定的数据行?

Java 如何从testng数据提供程序检索特定的数据行?,java,testng,Java,Testng,我有一个数据提供商,如下所示: @DataProvider(name = "therapistToDoList") public static Object[][] data(){ return new Object[][]{ {"06012017", "Low", "This is a low task description added via automation for therapist/admin."},

我有一个数据提供商,如下所示:

@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数据提供程序也可以这样做吗?

其中一种方法可能是:保留一个计数器,检查是否已达到目标数据,然后执行任务

演示代码:

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参数从testngsuitexml文件(or)中接受一个特定的行作为选择,然后相应地过滤数据提供程序的行

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;
    }
}
公共类过滤器提供示例{
@测试(dataProvider=“therapistToDoList”)
public void testWithSpecificDataFromDataProvider(字符串a、字符串b、字符串c){
Assert.assertNotNull(a);
Assert.assertNotNull(b);
Assert.assertNotNull(c);
}
@数据提供者(name=“therapistToDoList”)
公共静态对象[][]数据(ITestContext ctx){
对象[][]数据=新对象[][]{
{“06012017”,“低”,“这是通过治疗师/管理员自动化添加的低任务描述。”},
{“06012017”,“中等”,“这是通过治疗师/管理员自动化添加的中等任务描述。”},
{“06012017”,“高”,“这是通过治疗师/管理员自动化添加的高任务描述。”},
};
返回filterData(数据,ctx.getCurrentXmlTest());
}
私有静态对象[][]过滤器数据(对象[][]数据,XmlTest XmlTest){
//如果该值是作为JVM参数给出的呢。
int rowFromJVM=Integer.parseInt(System.getProperty(“行”,“-1”);
if(rowFromJVM>0&&rowFromJVM0&&row
如果你使用这个工具,你应该坚持它所遵循的理念。数据提供程序用于为每个数据行运行测试


如果您需要一些不同的数据,那么创建一个不同的数据提供者。它可以调用初始值并从中筛选值。或者更好-可能会有一个小的数据提供程序,然后是一个大的数据提供程序,其中包含较小数据提供程序中的值并添加自己的行。

如果不使用这些行,为什么不注释或删除这些行?您的解决方案正在执行我预期的步骤,即仅使用一行数据提供程序执行测试,然而,测试结果显示测试执行了三次,这并不好,因为我希望测试只在特定行中运行一次。