如何在testng中使用@dataprovider从excel读取不同的数据类型

siv3szwd  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(476)

我正在尝试从excel获取我的测试用例的输入。我的输入包含字符串和数字值。我知道如何使用下面的代码检索单个数据类型的值

public Object[][]  readnumericvalue() throws IOException {
    File src = new File("filepath");    
    FileInputStream fis = new FileInputStream(src);
    XSSFWorkbook    wb = new XSSFWorkbook(fis);
    XSSFSheet sheet1 = wb.getSheetAt(1);
    XSSFRow row = sheet1.getRow(0);
    int rowcount = sheet1.getLastRowNum();
    int columnCount = row.getLastCellNum();
    Object data1[][]=new Double[rowcount+1][columnCount];
    columnCount = columnCount-1;

    for(int i=0;i<=rowcount;i++) {
        for(int j=0;j<=columnCount;j++) {
            data1[i[j]= sheet1.getRow(i).getCell(j).getNumericCellValue();
        }
    }
    return data1;
}

我尝试在单个数据提供程序中读取这两种数据类型,但不知道如何为多个数据类型初始化2d数组

File src = new File("");

FileInputStream fis = new FileInputStream(src);
XSSFWorkbook    wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
XSSFRow row = sheet1.getRow(0);
int rowcount = sheet1.getLastRowNum();
int columnCount = row.getLastCellNum();     
Object data1[][]=new String[rowcount+1][columnCount];
columnCount = columnCount-1
for(int i=0;i<=rowcount;i++) {
    for(int j=0;j<=columnCount;j++) {
        Cell cell = row.getCell(j);
        switch (cell.getCellTypeEnum()) {
            case STRING:
                data1[i][j]=sheet1.getRow(i).getCell(j).getStringCellValue();
                break;

            case NUMERIC:               
                data1[i][j]=sheet1.getRow(i).getCell(j).getNumericCellValue();
                break;                              
        }
    }   
}
return data1;
neekobn8

neekobn81#

如果希望在同一个数据提供程序中容纳这两种数据类型,那么基本上可以将数组定义为对象数组。
这是一个样本。
假设excel工作簿如下所示

| Name | Age |
|------|-----|
| Jack | 24  |
| Jill | 23  |
| Bob  | 30  |

下面是一个示例代码,它读取这些数据

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

public class SampleTestClass {

    @Test(dataProvider = "dp")
    public void testMethod(String name, int age) {
        System.err.println("Name :" + name + ", Age :" + age);
    }

    @DataProvider(name = "dp")
    public Object[][] readnumericvalue() throws IOException {
        File src = new File("src/test/resources/47036541.xlsx");
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1 = wb.getSheetAt(0);

        int rowcount = sheet1.getPhysicalNumberOfRows();
        int columnCount = sheet1.getRow(0).getLastCellNum();
        Object objects[][] = new Object[rowcount-1][columnCount];
        int rowCounter = 0;

        Iterator<Row> rowIterator = sheet1.iterator();
        boolean firstRow = true;
        while (rowIterator.hasNext()) {
            Row currentRow = rowIterator.next();
            if (firstRow) {
                firstRow = false;
                continue;
            }
            Iterator<Cell> cellIterator = currentRow.iterator();
            int colCounter = 0;
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        objects[rowCounter][colCounter] = cell.getStringCellValue();
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        objects[rowCounter][colCounter] =  new Double(cell.getNumericCellValue()).intValue();
                        break;
                }
                colCounter++;
            }
            rowCounter++;
        }
        return objects;
    }
}

这是输出

Name :Jack, Age :24
Name :Jill, Age :23
Name :Bob, Age :30

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

相关问题