1
正在從csv文件讀取數據,我測試了這些數據將作爲輸入。 我希望它作爲tes套件運行每一組值。對於正在使用的數據提供 的問題是,它是隻取數據的最後一組行,請幫我調試的代碼如何從csv文件傳遞參數給testng中的數據提供者
For eg : if my csv has following data
name1 id1 text1
name2 id2 text2
name3 id3 text3
它只取最後一行NAME3 ID3文字3和運行測試只有一次不是三次。
@DataProvider(name = "test")
public Object[][] provider() throws InterruptedException
{
Object[][] returnObject ;
String[] checkpoint = ReadfromCSV();
count = count + 1;
returnObject = new Object[][]{checkpoint };
return returnObject;
}
@Test(description = "Test", groups = "test" , dataProvider = "test")
public void compare(String val1,String val2,String val3,String val4,String val5,String val6,String val7,String val8,String val9,String val10,String val11) {
System.out.println("1:" + val1);
System.out.println("4:" + val2);
System.out.println("5:" + val3);
}
@SuppressWarnings("null")
public String[] ReadfromCSV() throws InterruptedException {
String[] data= null;
String csvFile = "F:/sample1.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
data= line.split(cvsSplitBy);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
return data;
}
感謝您的回覆,我沒有得到您的想法。你可以在我的代碼中進行更改並顯示。 – Dude 2014-09-25 09:45:57
@Dude我更新了我的答案。我懷疑你錯過了TestNg只需調用一次數據提供者來準備多個測試運行的測試用例的事實。 – luboskrnac 2014-09-25 11:01:15