本文介绍了将 Array 从 dataprovider 返回到 @Test 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 TestNG 创建了一个 Selenium 框架.在此@Dataprovider 中包含一组代码,用于从excel 读取数据并将此值以数组形式返回给@test.

I have created a Selenium framework with TestNG. In this @Dataprovider contain set of code that reads data from excel and return this value in arrayform to @test.

@Test(dataProvider="createData1")
private  void login (String data, String data1) throws IOException{}

@DataProvider
public static String [][] createData1() throws IOException{

     String Scenarioname=new Object(){}.getClass().getEnclosingClass().getSimpleName();
     return  xls.readvalues(Scenarioname,"testdata");

}

因为 login 测试用例有两个输入,所以方法 Login 有两个属性.同样,如果注册测试用例有 5 个输入,那么注册方法将有 5 个属性.但是我想以某种方式创建一种标准方式,以便当我从@dataprovider 返回数组时.然后@test 方法应该动态读取值,而不是我提到每个方法的属性数量.

Since login Test case has two inputs so method Login has two attributes.Likewise if register Test case has 5 input then register method will have 5 attributes.But I want to create a standard way in a manner so that when I return array from @dataprovider. Then @test method should read value dynamically rather than I mentioned number of attributes for each method.

推荐答案

你的 @DataProvider 可以返回 Object[][] 而不是 String[][].因此,您可以在第一位返回场景规范,在第二位返回带有用户数据的对象.类似的东西:

Your @DataProvider can return Object[][] instead of String[][]. So you can return scenario specification on first place and Object with user data on second place. Something like:

return new Object[][] {
    {"My Scenario1", new MyUserDataObject("name1", "password1", "male")},
    {"My Scenario2", new MyUserDataObject("name2", "password2", "female")}
};

和测试方法签名将是:登录(字符串场景名称,MyUserDataObject userData)

and test method signature will be:login(String scenarioName, MyUserDataObject userData)

然后测试可以从 userData 参数中读取所有必需的数据.

Then test can read all mandatory data from userData parameter.

参数化测试的参数值很简单,因此没有 userData[0][0] (http://testng.org/doc/documentation-main.html#parameters-dataproviders)

Parametrized test got simple value as parameter so there is no userData[0][0] (http://testng.org/doc/documentation-main.html#parameters-dataproviders)

@BeforeMethod 在 TestNG 中不支持 @DataProviders (https://groups.google.com/forum/#!topic/testng-users/3Kny3qTVdmg)

@BeforeMethod doesn't support @DataProviders in TestNG (https://groups.google.com/forum/#!topic/testng-users/3Kny3qTVdmg)

这篇关于将 Array 从 dataprovider 返回到 @Test 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:15