使用数据驱动测试TestNG中的Apache POI [英] Datadriven Testing in TestNG using Apache POI

查看:123
本文介绍了使用数据驱动测试TestNG中的Apache POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来TestNG的框架。请指导如何使用Apache POI(Excel)中参数化测试用例。

I am new to TestNG framework. Please guide how to parameterize the test cases using Apache POI(Excel).

我有一个code从Excel中第二行读取。

I have a code to read from second row from Excel.

public class spreadData {

private transient Collection data = null;

public spreadData(final InputStream excelInputStream) throws IOException {
    this.data = loadFromSpreadsheet(excelInputStream);
}

public Collection getData() {
    return data;
}

private Collection loadFromSpreadsheet(final InputStream excelFile)
        throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook(excelFile);

    data = new ArrayList();
    Sheet sheet = workbook.getSheetAt(0);

    int numberOfColumns = countNonEmptyColumns(sheet);
    List rows = new ArrayList();
    List rowData = new ArrayList();

   /*for (Row row : sheet) {
        if (isEmpty(row)) {
            break;
        } else {
            rowData.clear();
            for (int column = 0; column < numberOfColumns; column++) {
                Cell cell = row.getCell(column);
                rowData.add(objectFrom(workbook, cell));
            }
            rows.add(rowData.toArray());
        }
    }*/


    int rowStart = 1;
 //int rowEnd = Math.max(1400, sheet.getLastRowNum());
    for (int rowNum = rowStart; rowNum <=  sheet.getLastRowNum(); rowNum++) {
        //Row r = sheet.getRow(rowNum);
        Row read = sheet.getRow(rowNum);
        if (isEmpty(read)) {
            break;
        } else {
            rowData.clear();
            for (int column = 0; column < numberOfColumns; column++) {

                Cell cell = read.getCell(column);
                rowData.add(objectFrom(workbook, cell));
            }
            rows.add(rowData.toArray());
        }
    }
    return rows;
}

private boolean isEmpty(final Row row) {
    Cell firstCell = row.getCell(0);
    boolean rowIsEmpty = (firstCell == null)
            || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
    return rowIsEmpty;
}

/**
 * Count the number of columns, using the number of non-empty cells in the
 * first row.
 */
private int countNonEmptyColumns(final Sheet sheet) {
    Row firstRow = sheet.getRow(0);
    return firstEmptyCellPosition(firstRow);
}

private int firstEmptyCellPosition(final Row cells) {
    int columnCount = 0;
    for (Cell cell : cells) {
        if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            break;
        }
        columnCount++;
    }
    return columnCount;
}

private Object objectFrom(final HSSFWorkbook workbook, final Cell cell) {
    Object cellValue = null;

    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        cellValue = cell.getRichStringCellValue().getString();
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        cellValue = getNumericCellValue(cell);
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        cellValue = cell.getBooleanCellValue();
    } else if (cell.getCellType()  ==Cell.CELL_TYPE_FORMULA) {
        cellValue = evaluateCellFormula(workbook, cell);
    }

    return cellValue;

}

private Object getNumericCellValue(final Cell cell) {
    Object cellValue;
    if (DateUtil.isCellDateFormatted(cell)) {
        cellValue = new Date(cell.getDateCellValue().getTime());
    } else {
        cellValue = cell.getNumericCellValue();
    }
    return cellValue;
}

private Object evaluateCellFormula(final HSSFWorkbook workbook, final Cell cell) {
    FormulaEvaluator evaluator = workbook.getCreationHelper()
            .createFormulaEvaluator();
    CellValue cellValue = evaluator.evaluate(cell);
    Object result = null;

    if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result = cellValue.getBooleanValue();
    } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        result = cellValue.getNumberValue();
    } else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
        result = cellValue.getStringValue();   
    }

    return result;
}

}

我的问题是如何使用TestNG参数化测试用例?通过使用@dataProvider TestNG的注解装置。
请帮我配个样品code或解释。

My question is how to parameterize the test cases using TestNG? means by using @DataProvider TestNG annotation. Kindly help me with a sample code or explanation.

推荐答案

我不知道哪个设定您想要的数据...但这里有一个dataProvider中是如何工作的:

I'm not sure which set of data you want...but here's how a dataProvider works:

说我有一个测试,需要一个字符串,然后像这样一个int:

Say I have a test that takes a String and then an int like this:

@Test(dataProvider = "excelData")
public void executeTest(String name, int value){}

我的数据提供者将是这个样子:

My data provider would look something like this:

@DataProvider(name = "excelData")
public Object[][] data(){
    return new Object[][]{
               {"Test",1},
               {"More Testing",7},
               {"Last Test",-5}}
}

该测试将运行3次,与阵列中的每一行是一组是要传递的数据。你需要将Excel数据转换成这样的格式。

The test will be run 3 times, with each row of the array is the set of data that is to be passed in. You will need to convert your excel data into such a format.

请注意,您也可以返回一个的Iterator&LT;对象[]方式&gt; 如果您preFER

Note, you can also return an Iterator<Object[]> if you prefer.

这篇关于使用数据驱动测试TestNG中的Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆