您可以將數據提供者設置爲Object數組,並使用ArrayList>將您的參數設置爲鍵值對。
@DataProvider
public Object[][] getTestData()
{
List<HashMap<String, String>> arrayMapList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hashMapItems = new HashMap<String, String>();
//use a loop to fill in all the parameter name and value pairs
hashMapItems.put("parameterName1", "parameterValue");
hashMapItems.put("parameterName2", "parameterValue");
//--------------More put statements here------
//finally add hash map to the list
arrayMapList.add(hashMapItems);
//Iterate the array list and store each HashMap object in an object array. First dimension is the iterator value.
Object [][] hashMapObj = new Object [arrayMapList.size()][1];
for(int i=0; i<arrayMapList.size() ; i++) {
hashMapObj[i][0] = searchStrings.get(i);
}
return hashMapObj;
}
在陣列列表中的每個散列映射值,試驗方法將其自己的一組參數來運行
@Test (dataProvider = "getTestData", enabled = true)
public void testDataRead(HashMap<String,String> hashMapValue)
{
System.out.println(hashMapValue.get(parameterNameKey)); //parameter 1
System.out.println(hashMapValue.get(parameterNameKey)); //parameter 2
}
如果你的方法需要10個參數,需要用10個參數來聲明它。 – 2013-04-30 16:35:41
@CedricBeust如果我們在測試方法中聲明瞭超過10個參數,那麼代碼看起來有些奇怪,我想知道是否有任何方法可以在測試方法的參數列表部分動態聲明這些參數數量。 – 2013-05-02 04:31:30
然後將它們傳遞給對象。無論哪種方式,這是一個Java問題,而不是TestNG。 – 2013-05-02 18:19:01