使用@Factory TestNG 时将参数传递给 dataProvide
Pass a parameter to dataProvide when using the @Factory TestNG
有一个异常使用 TestNG 的@Factory 和@dataProvider 注释无法传递调用测试名称,这在构建通用测试作为每次提供不同数据的框架时需要(来自 Excel).在数据提供者处使用方法 getName() 会导致 运行 次异常。 getName() 函数仅在使用 @dataprovider 时有效。但是与 @Factory 结合使用时会发生异常。有没有办法解决或绕过这个问题?
package Tests;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public abstract class GenericFactory11 {
protected List<String> data;
public GenericFactory11(List<String> data) {
this.data = data;
}
@DataProvider(name = "getDataForInstances")
public static Object[][] getDataForInstances(ITestContext context,Method m){
System.out.println(context.getName());
System.out.println(m.getName()); // THIS Line Causes the exception
return new Object[][]{
{Collections.singletonList("Java")},
{Arrays.asList("TestNG", "JUnit")},
{Arrays.asList("Maven", "Gradle", "Ant")}
};
}
}
package Tests;
import static org.testng.Assert.assertNotEquals;
import java.util.List;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class Sanity11 extends GenericFactory11 {
@Factory (dataProvider = "getDataForInstances")
public Sanity11(List<String> data) {
super(data);
}
@Test
public void Sanity(){
String text = this.data.get(this.data.size()-1);
System.out.println("Printing Parameters when running test method [" + text + "]");
assertNotEquals(text,"");
}
}
运行代码收到以下错误:
java.lang.RuntimeException: java.lang.NullPointerException
在 org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49)
您看到 NullPointerException
是因为您的数据提供者声明它将接受 java.lang.reflect.Method
对象,但在这种情况下,调用方法是 java.lang.reflect.Constructor
而不是 Method
对象。
您应该将 java.lang.reflect.Method
替换为 org.testng.ITestNGMethod
。
修改后的数据提供者如下所示:
@DataProvider(name = "getDataForInstances")
public static Object[][] getDataForInstances(ITestContext context, ITestNGMethod method) {
System.out.println("test name = " + context.getName());
System.out.println("Method name = " + method.getConstructorOrMethod().getName() + "()\n");
return new Object[][] {
{Collections.singletonList("Java")},
{Arrays.asList("TestNG", "JUnit")},
{Arrays.asList("Maven", "Gradle", "Ant")}
};
}
有一个异常使用 TestNG 的@Factory 和@dataProvider 注释无法传递调用测试名称,这在构建通用测试作为每次提供不同数据的框架时需要(来自 Excel).在数据提供者处使用方法 getName() 会导致 运行 次异常。 getName() 函数仅在使用 @dataprovider 时有效。但是与 @Factory 结合使用时会发生异常。有没有办法解决或绕过这个问题?
package Tests;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public abstract class GenericFactory11 {
protected List<String> data;
public GenericFactory11(List<String> data) {
this.data = data;
}
@DataProvider(name = "getDataForInstances")
public static Object[][] getDataForInstances(ITestContext context,Method m){
System.out.println(context.getName());
System.out.println(m.getName()); // THIS Line Causes the exception
return new Object[][]{
{Collections.singletonList("Java")},
{Arrays.asList("TestNG", "JUnit")},
{Arrays.asList("Maven", "Gradle", "Ant")}
};
}
}
package Tests;
import static org.testng.Assert.assertNotEquals;
import java.util.List;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class Sanity11 extends GenericFactory11 {
@Factory (dataProvider = "getDataForInstances")
public Sanity11(List<String> data) {
super(data);
}
@Test
public void Sanity(){
String text = this.data.get(this.data.size()-1);
System.out.println("Printing Parameters when running test method [" + text + "]");
assertNotEquals(text,"");
}
}
运行代码收到以下错误:
java.lang.RuntimeException: java.lang.NullPointerException 在 org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49)
您看到 NullPointerException
是因为您的数据提供者声明它将接受 java.lang.reflect.Method
对象,但在这种情况下,调用方法是 java.lang.reflect.Constructor
而不是 Method
对象。
您应该将 java.lang.reflect.Method
替换为 org.testng.ITestNGMethod
。
修改后的数据提供者如下所示:
@DataProvider(name = "getDataForInstances")
public static Object[][] getDataForInstances(ITestContext context, ITestNGMethod method) {
System.out.println("test name = " + context.getName());
System.out.println("Method name = " + method.getConstructorOrMethod().getName() + "()\n");
return new Object[][] {
{Collections.singletonList("Java")},
{Arrays.asList("TestNG", "JUnit")},
{Arrays.asList("Maven", "Gradle", "Ant")}
};
}