【问题标题】:How to call Dataprovider from my Test method with parameters如何使用参数从我的测试方法中调用 Dataprovider
【发布时间】:2015-10-12 12:04:44
【问题描述】:
    @DataProvider (name=getData)
                public static Object[][] getData(){
                    Excelreader excel = new Excelreader("C:\\WorkspaceExcel\\Datasource.xlsx"); private static
        String sheetName="SmokeTest"; // Test Data stored in a a table with
        columns and rows. Ex. UserNmae and password are the columns and data
        for them in rows  private static String tableName="CreatePolicy";

        // Some logic to read the data table store the data in a 2
        dimentional array.

        // Some logic by which I store the columns as Keys and data for them
        as values in a HASHTABLE  Return the hashtable 
    }




       @Test (dataProvider="getData") 
        public void testData(Hashtable<String, String> data){
        /* Logic to read an .xlsx file which has Snenario name, TestCase Name  and its corespnding runflag. If the runflag is true i need to read the scenario name and testcase name from the .xlsx file.  */

/* I need some logic to pass the ScenarioName (equivalent to Sheetname in DP) and TestCase Name (equivalent to datatable name in DP) which i am getting by reading the excelfile to DataProvider, so i can get the data which i need to execute the test case */

/*I am doing the above as part of Hybridframework, where i have Scenario, TC, Test step details in one excel sheet and data for each testcase in one more sheet */
       }

我的问题: 我想要一些逻辑,所以在执行@Test 时,我应该动态传递数据文件路径、SheetName、TableName,这样可以使用相同的数据提供程序,这将为我提供不同的数据集。

注意:数据提供者将以哈希表的形式返回在 excel 中以表格格式指定的数据,并带有表名。因此,如果将工作表路径、工作表名称和表名传递给数据提供者,那么我的 DP 将读取该表并以哈希表的形式返回整个数据表。

【问题讨论】:

    标签: java testng testng-dataprovider


    【解决方案1】:

    我建议您通过 XML 提供参数,如 Parameters from testng.xml 中所述

    【讨论】:

    • 我想知道我们如何通过 XML 参数化来实现这一点。在 Excel 工作表中,每个数据表都有多个列。与 dataprovider 的情况一样,我们必须在 @Test 方法中使用匹配数量的参数(这里是它的倍数),以避免我使用哈希表。因此,无论数据提供者返回多少参数,我的测试方法都不需要添加这些参数。无论数据表中的列数和行数如何,我的 DP 都会返回数据。但我唯一关心的是如何动态传递 DP 为其返回数据的 sheetname 和 datatablename。
    【解决方案2】:

    如果您只是希望能够重用获取测试数据的复杂逻辑,为什么不将其移至辅助函数并在单独的数据提供者中传递您的参数?

    public class ReusableDataprovider {
        @Test(dataProvider = "data_from_table1")
        public void test1(Hashtable<String, String> data) {
            Assert.assertEquals(data.get("Username"), "user_table1", "Wrong username");
            Assert.assertEquals(data.get("Password"), "pass_table1", "Wrong password");
        }
    
        @Test(dataProvider = "data_from_table2")
        public void test2(Hashtable<String, String> data) {
            Assert.assertEquals(data.get("Username"), "user_table2", "Wrong username");
            Assert.assertEquals(data.get("Password"), "pass_table2", "Wrong password");
        }
    
        @DataProvider
        protected Object[][] data_from_table1() {
            return fetchData("file1", "sheet1", "table1");
        }
    
        @DataProvider
        protected Object[][] data_from_table2() {
            return fetchData("file2", "sheet2", "table2");
        }
    
        protected Object[][] fetchData(String filePath, String sheetName, String tableName) {
    
            final Hashtable<String, String> data = new Hashtable<String, String>();
            // Do all the complex excel logic here
            data.put("Username", "user_" + tableName);
            data.put("Password", "pass_" + tableName);
    
            return new Object[][] {{data}};
        }
    
    }
    

    【讨论】:

    • 我的测试方法读取一个带有 TCname、Runflag 的 .xlsx 文件,它会循环使用带有 yes 标志的测试用例的数量。对于每个 TC,它需要 .xlsx 文件中的数据,如果我可以动态传递工作表和表名,我的 DP 将返回数据。由于我的 TC 数量会有所不同,所以我不知道我可能需要多少 DP。所以我需要一个解决方案,它使用相同的 DP 并为不同的测试用例提供不同的数据集。
    • 你会有多少美味的案例?你说'动态',但他们的签名(接受的参数)是什么?我不完全理解。也许你可以提供一些示例数据。
    • 我在 .xlsx 文件中有测试用例名称、Scenarionames 及其运行标志,其数量等于我在执行之前在 .xlsx 文件中创建的条目数。因为我在@Test mathod 中使用hashtable 作为参数并且我的DP 也返回一个hash 表,所以我不会担心我需要在使用DP 的@test 方法中传递的参数数量。请参考我的问题中的@Test方法,我已经写了我将在该测试方法中执行的操作。
    • 对不起,我真的很努力,但是我很难理解你的问题。我不知道你为什么要从你的测试中调用 DP。 DP 应该注入参数,因此请确保 DP 为测试提供所有必要的参数。
    • 这是我混合框架的一部分。因此,在我的驱动程序脚本中,我正在读取一个 .xlsx 文件以获取我想要执行的场景名称和测试用例名称。所以我想要一些逻辑将这些值传递给 DP 以获取我将用来执行测试步骤的测试数据。所以我将拥有我的驱动程序脚本和 DP,我将使用它们来执行所有测试用例。
    【解决方案3】:

    您可以使用以下方式:

    方法一: 1.为Path、SheetName、TableName创建类变量。 2. 使用构造函数初始化这些变量 3. 将这些变量直接调用到您的数据提供程序函数中。

    方法 2: 1. 使用 System 类在系统属性 env 中设置 Path、SheetName、TableName,或者您可以使用 maven、gradle 构建工具进行设置。 2. 使用 System getProperty 方法在您的数据提供者类中获取这些属性值。

    【讨论】:

    • 方法 1 对我来说听起来不错,但是我不知道如何实现它,如果你能举一个例子那就太好了。实际上,我正在读取一个 .xlsx 文件以获取路径、工作表名和数据表名(TC 名称),并且对于每个 @test 方法,我想使用这些参数调用 DP 以获取数据。所以如果可能的话,请举一个例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多