簡體   English   中英

如何從testng數據提供程序檢索特定的數據行?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM