我正在尝试创建一个testNG dataprovider方法,该方法将从我的项目中返回自定义类的对象数组。这个数组是一维数组,我不想从这个dataprovider方法返回二维数组。如果Dataprovider总是返回2D数组(不小于或大于2D),请建议。如果是,那么我需要帮助,如下所示:
返回新的对象[] [] {{user [0]},{user [1]},{user [2]},{user [3]}}
我们能否以更好的方式编写这行代码,因为如果以后这个数组扩展将包含超过4个元素,我们将不得不编辑这个完整的函数,我们不能使用list等吗?
以下是我目前使用的代码:
@DataProvider(name = "credentialsProvider", parallel=false)
public static Object[][] credentialsProvider() throws Exception {
User[] user=new User[4];
user[0]=new User(UserTypes.AGENCY_MANAGER,1);
user[1]=new User(UserTypes.AGENT,1);
user[2]=new User(UserTypes.AGENCY_MODERATOR,1);
user[3]=new User(UserTypes.EW_ECS_AGENCY_MANAGER,1);
return new Object[][]{{user[0]},{user[1]},{user[2]},{user[3]}};
};
}
答案 0 :(得分:0)
There is a reason for the Dataprovider to be 2D - today you have one string you want to pass as data, if you had to pass let's say a User object and a UserDetails object to a testcase, you would need a way to pass both and hence the unit array is basically what you need to pass to single test and the 2d is for a list of such multiple data.
Why don't you just loop over your user array to add the user objects instead of going over by indices in one line. If something gets added, you just need to define the user object, the for loop would then loop over the entire length.