2017-01-06 9 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."}, 
     }; 
    } 

私は、このデータプロバイダを使用して私のテストケースを実行するにはどうすればよいが、1つの特定の行のためではなく、それらのすべてを?

これはtestngデータプロバイダでも可能ですか?

+0

これらの行を使用しないとコメントや削除はしないでください。 –

答えて

0

方法の1つは、カウンタを保持し、ターゲットデータに達しているかどうかを確認してから、タスクを実行することです。

デモコード:ここ

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

あなたのソリューションは私の意図したステップを実行していましたが、これはdataproviderの1行だけでテストを実行していましたが、テスト結果はテストが3回実行されたことを示していました。特定の行で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

ツールを使用する場合、それに続くイデオロギーに固執する必要があります。データプロバイダごとに、各データ行のテストを実行する必要があります。

異なるデータが必要な場合は、別のDataProviderを作成します。それは最初のものを呼び出し、そこから値をフィルタリングすることができます。またはそれより優れている場合は、小さなDataProviderが存在し、小さい方の値が含まれ、それ自身の行が追加される大きな値が存在する可能性があります。

関連する問題