我想将参数传递给@test以使用不同的数据运行多次。所以我使用 @DataProvider 来重新调整二维对象数组。我有一维字符串,所以首先我将其转换为二维数组,然后为对象赋值。 。但出现以下异常。
public class DtaProvider {
public static String patchfileName = null;
public static String[] patchsplit = null;
public static String temp= null;
public int number = 0;
@DataProvider(name = "getData")
public Object[][] createData() {
patchfileName = "hi,how,are,you";
patchsplit = patchfileName.split(",");
Object[] arr = patchsplit ;
System.out.println(arr.length);
for(int i=0;i<arr.length;i++){
System.out.println(arr[i].toString());
}
Object[][] data = new Object[arr.length][arr.length];
for (int x = 0; x < arr.length; x++){
data[x][x] = arr[x];
}
return data;
}
@Test(dataProvider="getData")
public void DownloadPatch(String patchfileNamea ){
try{
System.out.println("Name is b"+patchfileNamea);
}
catch (Exception e){
org.testng.Assert.fail("Failed to Download Patch to NgDriver " + e.getMessage());
}
}
}
TestNG] Running:
C:\Users\Mohan Raj S\AppData\Local\Temp\testng-eclipse-1472908796\testng-customsuite.xml
4
hi
how
are
you
FAILED: DownloadPatch
org.testng.TestNGException:
The data provider is trying to pass 4 parameters but the method testclasses.DtaProvider#DownloadPatch takes 1
at org.testng.internal.Invoker.injectParameters(Invoker.java:1257)
请帮助我如何将参数按顺序传递给我的@test方法?
最佳答案
错误日志中明确提到,@Test 方法仅采用一个参数,而 @DataProvider 返回四个参数。
很简单,@DataProvider 返回的参数数量相同,应该在 @Test 方法中传递。您的 @Test 方法应如下所示:
@Test(dataProvider="getData")
public void DownloadPatch(String param1, String param2, String param3, String param4){
System.out.println(param1);
}
更新:1
即使您出于某种原因只在 @Test 方法中传递一个参数,那么您也需要以这种方式修改您的 @DataProvider。
public Object[][] createData() {
patchfileName = "hi,how,are,you";
patchsplit = patchfileName.split(",");
Object[][] data = new Object[patchsplit.length][1];
for (int x = 0; x < patchsplit.length; x++) {
data[x][0] = patchsplit[x];
}
return data;
}
关于java - 我正在尝试使用 @DataProvider 来运行具有不同参数的 @test,但出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33202393/