【问题标题】:Data Providers for BeforeMethod capabilitiesBeforeMethod 功能的数据提供者
【发布时间】:2021-08-10 02:59:53
【问题描述】:

有没有办法为BeforeMethod 函数使用数据提供者? 我想对许多不同的设备运行并行测试,所以我想使用参数来设置功能。但我想要一种不同于使用testng.xml 的方式。

【问题讨论】:

    标签: java testng appium


    【解决方案1】:

    您可以通过Object[] 参数在您的BeforeMethod 中获取数据提供者传递给测试的数据。

    @BeforeMethod
    public void beforeMethod(Object[] data) {
        //.......
    }
    

    假设以下代码:

    @Test(dataProvider = "dataOne")
    public void testMethodOne(String one, int two) {
    }
    
    @Test(dataProvider = "dataTwo")
    public void testMethodTwo(int one) {
    }
    
    @DataProvider
    public Object[][] dataOne() {
        return new Object[][]{ {"a", 1} };
    }
    
    @DataProvider
    public Object[][] dataTwo() {
        return new Object[][]{ {1} };
    }
    

    为了在数据提供者传递的数据到达测试方法之前得到它,你定义了一个如下的 before 方法。我也添加了Method m 参数。这将有助于识别正在运行的测试用例。 Object[] data 包含数据提供者传递的数据。如果你添加了这个参数,那么 testNG 会自动将数据传递给 before 方法。

    @BeforeMethod
    public void beforeMethod(Method m, Object[] data) {
        if(m.getName().equals("testMethodOne")) {
            String x = (String) data[0];
            int y = (int) data[1];
        } else if(m.getName().equals("testMethodTwo")) {
            int x = (int) data[0];
        }
    }
    

    【讨论】:

    • 对不起,我不明白,可以举一个小例子吗?提前谢谢你。
    • @laxsore 您希望将数据从数据提供者传递给您的测试用例到您的 beforeMethod 对吗?
    • 没错,但我不知道如何从 Object[]data 传递数据。能举个例子吗?
    • @laxsore 我在答案中添加了一些相关信息以使其更加清晰。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2020-08-07
    • 2021-12-24
    相关资源
    最近更新 更多