【问题标题】:testNG multiple dataproviders passing data to one class having multiple test methodstestNG 多个数据提供者将数据传递给具有多个测试方法的类
【发布时间】:2018-04-06 12:51:20
【问题描述】:

EDIT1:我有一个 ExcelUtility.java 类来从中获取单元格数据并将其传递给我的测试类中的测试方法。

我正在读取 1 个 excel 文件。 excel文件有3个工作表。 每个工作表内部都有 1 个独特的工作表。 我有 1 个测试班。 测试类有 3 种测试方法。 测试类包含 3 个数据提供者。 所有数据提供者的工作表名称和工作表名称都不同。

测试类中的测试是这样写的:

  @Test(priority = 0, dataProvider = "dp1")
    public void test01(String...strings){
 }
 @Test(priority = 1, dataProvider = "dp2")
    public void test02(String...strings){
 } 
 @Test(priority = 2, dataProvider = "dp3")
    public void test03(String...strings){
}

我有以下 java 类可以使用 apache poi jar 从 XLSX 文件中读取:

public class ExcelUtility {

        private static XSSFWorkbook ExcelWBook;
        private static XSSFSheet ExcelWSheet;

        /*
         * Set the File path, open Excel file
         * @params - Excel Path and Sheet Name
         */
        public static void setExcelFile(String path, String sheetName) throws Exception {
            try {
                // Open the Excel file
                FileInputStream ExcelFile = new FileInputStream(path);

                // Access the excel data sheet
                ExcelWBook = new XSSFWorkbook(ExcelFile);
                ExcelWSheet = ExcelWBook.getSheet(sheetName);
            } catch (Exception e) {
                throw (e);
            }
        }

        public static String[][] getTestData(String tableName) {
            String[][] testData = null;

            try {
                // Handle numbers and strings
                DataFormatter formatter = new DataFormatter();
                XSSFCell[] boundaryCells = findCells(tableName);
                XSSFCell startCell = boundaryCells[0];

                XSSFCell endCell = boundaryCells[1];

                int startRow = startCell.getRowIndex() + 1;
                int endRow = endCell.getRowIndex() - 1;
                int startCol = startCell.getColumnIndex() + 1;
                int endCol = endCell.getColumnIndex() - 1;

                testData = new String[endRow - startRow + 1][endCol - startCol + 1];

                for (int i=startRow; i<endRow+1; i++) {
                    for (int j=startCol; j<endCol+1; j++) {
                        // testData[i-startRow][j-startCol] = ExcelWSheet.getRow(i).getCell(j).getStringCellValue();
                        Cell cell = ExcelWSheet.getRow(i).getCell(j);
                        testData[i - startRow][j - startCol] = formatter.formatCellValue(cell);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return testData;
        }

        public static XSSFCell[] findCells(String tableName) {
            DataFormatter formatter = new DataFormatter();
            String pos = "begin";
            XSSFCell[] cells = new XSSFCell[2];

            for (Row row : ExcelWSheet) {
                for (Cell cell : row) {
                    // if (tableName.equals(cell.getStringCellValue())) {
                    if (tableName.equals(formatter.formatCellValue(cell))) {
                        if (pos.equalsIgnoreCase("begin")) {
                            cells[0] = (XSSFCell) cell;
                            pos = "end";
                        } else {
                            cells[1] = (XSSFCell) cell;
                        }
                    }
                }
            }
            return cells;
        }
    }

为了从 excel 文件中读取,我按以下方式组织了测试方法:

@DataProvider(name = "dp1")
    public Object[][] dp1() throws IOException {
        Object[][] testData = new Object[0][];
        try {
            ExcelUtility.setExcelFile(Constants.File_Path+Constants.File_Name, "Page1");
            testData = ExcelUtility.getTestData("P1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testData;
    }
    @DataProvider(name = "dp2")
    public Object[][] dp2() throws IOException {
        Object[][] testData = new Object[0][];
        try {
            ExcelUtility.setExcelFile(Constants.File_Path+Constants.File_Name, "Page2");
            testData = ExcelUtility.getTestData("P2");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testData;
    }
    @DataProvider(name = "dp3")
    public Object[][] dp3() throws IOException {
        Object[][] testData = new Object[0][];
        try {
            ExcelUtility.setExcelFile(Constants.File_Path+Constants.File_Name, "Page3");
           testData = ExcelUtility.getTestData("P3");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testData;
    }
    @Test(priority = 0, dataProvider = "dp1")
    public void test01(String...strings){
 //read data from excel and pass the value to the strings added as arguments in the method above
 }

 @Test(priority = 1, dataProvider = "dp2")
    public void test02(String...strings){

 }

 @Test(priority = 2, dataProvider = "dp3")
    public void test03(String...strings){

 }

我想做的是:

  1. 从sheet1读取第一行数据,传给test1,继续test2

  2. 从sheet2读取第一行数据,传给test2,继续test3

  3. 从sheet3读取第一行数据,传给test3,继续test1

  4. 从工作表 1 中读取第二行数据,将其传递给 test1,继续进行 test2

依此类推,取决于 Excel 表格中的行数。

会发生什么:

执行第一个测试,读取工作表 1,第 1 行。
执行第一个测试,读取工作表 1,第 2 行。
执行第二个测试,读取工作表 2,第 1 行。
执行第二个测试,读取工作表 2,第 2 行。

所有测试都失败了,因为它们相互依赖,这就是我设置执行优先级的原因。

我应该更改 Test 类中的某些内容,还是应该更改 ExcelUtility.java 类中的某些内容?

提前谢谢你!

【问题讨论】:

    标签: apache-poi testng testng-dataprovider


    【解决方案1】:

    查看与commit相关的文件

    它基于 xlsx 和测试数据创建 testng.xml。

    【讨论】:

    • 我已经编辑了问题,你能看看吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多