2017-01-06 77 views
0

我有一個數據提供者象下面這樣:如何從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."}, 
       {"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."}, 
     }; 
    } 

如何運行使用此數據提供我的測試案例,但只針對一個具體的行和不是所有的人?

這對測試數據提供者甚至可能嗎?

+0

爲什麼不評論或刪除這些行,如果你不使用它們? –

回答

0

其中一種方式可能是 - 您保留一個計數器並檢查您是否已達到目標數據,然後執行您的任務。

演示代碼:

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

} 
+0

您的解決方案正在執行我的預期步驟,該步驟僅使用1行數據提供程序執行測試,但是測試結果顯示測試執行了三次,這不太好看,因爲我希望測試僅運行一次與特定的行。 – hifzur

+0

我錯過了一些東西,希望你已經找到了它。 –

0

下面是通過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; 
    } 
} 
0

如果您使用該工具,您應該堅持它遵循的意識形態。 DataProviders在那裏爲每個數據行運行測試。

如果你需要一些不同的數據 - 然後創建一個不同的DataProvider。它可以調用最初的那個並從那裏過濾值。或者更好 - 可能會有一個小的DataProvider,然後是一個較大的DataProvider,它包含較小值的數據並添加它自己的行。

相關問題