Testng 如何使用@DataProvider将方法作为输入传递给@Test?

Testng 如何使用@DataProvider将方法作为输入传递给@Test?,testng,Testng,我一直在编写测试,通过获取包中所有@DataProvider方法的列表并运行它们来检查@DataProvider是否失败(因为这些测试会被无声地跳过)。这非常有效,但当我尝试使用@DataProvider(我知道是非常元的)实现它时,我在TestNG中遇到了一个似乎是错误的东西。在以下4种情况中,唯一有效的是封装式: package-mypackage; 导入org.testng.Assert; 导入org.testng.annotations.DataProvider; 导入org.testn

我一直在编写测试,通过获取包中所有@DataProvider方法的列表并运行它们来检查@DataProvider是否失败(因为这些测试会被无声地跳过)。这非常有效,但当我尝试使用@DataProvider(我知道是非常元的)实现它时,我在TestNG中遇到了一个似乎是错误的东西。在以下4种情况中,唯一有效的是封装式:

package-mypackage;
导入org.testng.Assert;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Test;
导入java.lang.reflect.InvocationTargetException;
导入java.lang.reflect.Method;
公共类TestDataProvidersWithMethod{
////////////////简单的例子:断言失败
@数据提供者(name=“TestThisDP”)
公共对象[][]testThisDP()引发IllegaAccessException、InstanceionException、InvocationTargetException、NoSuchMethodException{
类aclass=this.getClass();
Method=aclass.getMethod(“testThisDP”,(Class[])null;
返回新对象[][{new Object[]{method}};
}
@测试(dataProvider=“TestThisDP”)
public void testGiveMethod(方法方法)抛出IllegaAccessException、InstanceionException、InvocationTargetException{
System.err.println(“方法:”+Method.getName());
Assert.assertTrue(method.getName().equals(“testThisDP”);//失败:出于某种原因,名称实际上是“testGiveMethod”。
}
///////例如,这是可行的,但有额外的绒毛
类容器{
公共方法;
公开课;
公共容器(方法,类clazz){
这个方法=方法;
this.clazz=clazz;
}
}    
@数据提供程序(name=“TestThisDP4”)
公共对象[][]testThisDP4()引发IllegaAccessException、InstanceionException、InvocationTargetException、NoSuchMethodException{
类aclass=this.getClass();
Method=aclass.getMethod(“testThisDP”,(Class[])null;
返回新对象[][{new Object[]{new Container(方法,null)}};
}
@测试(dataProvider=“TestThisDP4”)
public void testGiveMethod(容器容器)抛出IllegaAccessException、InstanceionException、InvocationTargetException{
System.err.println(“方法:+container.Method.getName());
Assert.assertTrue(container.method.getName().equals(“testThisDP”);//成功!!
}
///////////////////奇怪的失败,由于类型不匹配,测试未运行
@数据提供程序(name=“TestThisDP2”)
公共对象[][]testThisDP2()引发IllegaAccessException、InstanceionException、InvocationTargetException、NoSuchMethodException{
类aclass=this.getClass();
Method=aclass.getMethod(“testThisDP2”,(Class[])null;
返回新对象[][{new Object[]{method,“}};
}
@测试(dataProvider=“TestThisDP2”)
public void testGiveMethod2(方法方法,字符串未使用)抛出IllegaAccessException、InstanceionException、InvocationTargetException{
System.err.println(“方法:”+Method.getName());
Assert.assertTrue(method.getName().equals(“testThisDP”);//失败:类型不匹配!!!
}
///////////////////////试图理解上述故障。。
///////////////////////从断言来看,这与普通示例一样失败
@数据提供程序(name=“TestThisDP3”)
公共对象[][]testThisDP3()引发IllegaAccessException、InstanceionException、InvocationTargetException、NoSuchMethodException{
类aclass=this.getClass();
Method=aclass.getMethod(“testThisDP3”,(Class[])null;
返回新对象[][{新对象[]{“”,方法};
}
@测试(dataProvider=“TestThisDP3”)
public void testGiveMethod3(字符串未使用,方法方法)引发IllegaccessException、InstanceionException、InvocationTargetException{
System.err.println(“方法:”+Method.getName());
Assert.assertTrue(method.getName().equals(“testThisDP”);//失败:出于某种原因,名称实际上是“testGiveMethod”。
}
}

我是做错了什么,还是这是TestNG中的一个bug?

这里没有bug。当您基本上将
方法作为
@Test
方法的参数之一时,TestNG基本上是尝试执行称为
本地注入的操作

因此,在您的例子中,TestNG基本上是注入一个
方法
引用,该引用表示当前“正在调用的”
@Test
方法

要禁用此
本机注入
,您需要使用注释
@NoInjection

这是相同测试代码的固定版本

import org.testng.Assert;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.NoInjection;
导入org.testng.annotations.Test;
导入java.lang.reflect.Method;
公共类TestDataProvidersWithMethod{
////////////////简单的例子:断言失败
@数据提供者(name=“TestThisDP”)
公共对象[][]testThisDP()引发异常{
类aclass=this.getClass();
Method=aclass.getMethod(“testThisDP”,(Class[])null;
返回新对象[][{new Object[]{method}};
}
@测试(dataProvider=“TestThisDP”)
public void testGiveMethod(@NoInjection Method){
System.err.println(“方法:”+Method.getName());
//失败:由于某种原因,名称实际上是“testGiveMethod”。
Assert.assertEquals(method.getName(),“testThisDP”);
}
///////例如,这是可行的,但有额外的绒毛
类容器{
公共方法;
公开课;
容器(方法,类clazz){
这个方法=方法;
this.clazz=clazz;
}
@凌驾
公共字符串toString(){
如果(clazz==nul