簡體   English   中英

將dataprovider參數作為數組存儲在testng中的測試方法中

[英]Store dataprovider parameters as array in test method in testng

我正在將excel數據寫入數組,並通過@dataprovider返回相同的數據。 代碼看起來像這樣-

for(i=0;i<numrow+1;i++){
      for (j=0;j<numcol;j++ ){
          if(xlsh.getRow(i).getCell(j)!=null){
              xldata[i][j]=xlsh.getRow(i).getCell(j).toString();
          }
          else{
              xldata[i][j]="";
          }
      }
}

return xldata;

我正在讀取的Excel大約有40列,我想在@test方法中將其寫入數組。 所以我嘗試在我的@test方法中使用以下代碼來讀取@dataprovider提供的數據

@Test(dataProvider = "name")
public void mymethod(String[] fromxl) {
 //code
}

但是以某種方式不能將數組識別為數組,並且我得到了錯誤提示-dataprovider有40個參數,但是測試方法只接受1。現在,如果我在測試方法中將數組更改為40個不同的參數(下面的示例代碼),它將似乎正在工作。 但是我不想這樣做,因為這很麻煩。 為什么無法使數組在我的測試方法中工作? 感謝任何對此的幫助/指針

@Test(dataProvider = "name")
public void mymethod(String data1, .. , String data40) {
 //code
}

要將數組作為參數傳遞給測試,您只需將其包裝在DataProvider使用的Object[][] 像這樣的事情應該做你想要的:

private String[] buildSingleDimensionArray() {
    String[] result = new String[40];
    for (int i = 0; i < result.length; i++) {
        result[i] = "element" + i;
    }
    return result;
}

@DataProvider(name = "arrayBuilder")
public Object[][] createData() {
    return new Object[][] { { buildSingleDimensionArray() }, };

}

@Test(dataProvider = "arrayBuilder")
public void theTest(String[] inputArray) {
    for (int i = 0; i < inputArray.length; i++) {
        System.out.println(inputArray[i]);
    }
}

您的測試僅需要一個參數-字符串數組。 因此,您的xldata應該以字符串數組的形式返回單個參數。

for(i=0;i<numrow+1;i++){

      for (j=0;j<numcol;j++ ){
          if(xlsh.getRow(i).getCell(j)!=null){
          //Build fresh array here instead of writing directly to return
               freshArr[j] = ...
          }
          else{
              freshArr[0] = "" ;
          }
//once all columns are read, set that to the return array
          xldata[i][0] = freshArr;
      }
}

這就是對我有用的-

for(i=0;i<numrow;i++){
  String[] newArray=new String[numcol]; 
  for (j=0;j<numcol;j++ ){
      if(xlsh.getRow(i).getCell(j)!=null){
          XSSFCell cellval=xlsh.getRow(i).getCell(j);
          cellval.setCellType(Cell.CELL_TYPE_STRING);
          newArray[j] = cellval.toString();
      }
      else{
          newArray[j] = "";
      }
  }
  xldata[i][0] = newArray;  
}
return xldata;

好吧,如果您仍在尋找答案,那么這里是解決方案:首先不確定您的excel數據電子表格是什么樣,但是您的for循環應該這樣說...

for(i=0;i<numrow+1;i++){
      for (j=0;j<numcol;j++ ){
          if(xlsh.getRow(i).getCell(j)!=null){
              xldata[I][j]=xlsh.getRow(i).getCell(j).getStringCellValue();
      }
      else{
          xldata[i][j]="";
      }
  }
}

return xldata;

並且當您擁有xldata [I] [j]時,您需要從此方法返回二維對象,例如“ return xldata”,並且您的方法應具有String [] []和返回類型。 例如:像這樣

public String[][] readExcelData(String filename, String sheetName, String tableName){
...
...
...
for(i=0;i<numrow+1;i++){
      for (j=0;j<numcol;j++ ){
      if(xlsh.getRow(i).getCell(j)!=null){
          xldata[i][j]=xlsh.getRow(i).getCell(j).getStringCellValue();
      }
      else{
          xldata[i][j]="";
      }
  }
}

return xldata;

}

您的測試方法還應該期望相同的行和列數據..諸如此類的情況取決於您從電子表格中獲取的數據是否也可以是2個參數或更多。

Ex:
@Test(dataProvider = "name")
public void test1(String row, String col){
..
}

嘗試這樣的實現,希望它能起作用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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