[英]A way of injecting TestNG test parameters into custom method invoked by IInvokedMethodListener
@BeforeMethod可以具有Object []参数,该参数注入提供给测试方法的参数
public class SampleTest {
@DataProvider
public Object[] one(){ return new Object[]{ 1 };}
@BeforeMethod
public void setUp(Object[] params){
Assert.assertEquals(params[0], 1);
}
@Test(dataProvider = "one")
public void test(Integer intParam){
//test smth
}
}
我想将这样的参数注入我的自定义方法中,该方法由IInvokedMethodListener调用
public class MethodInGroupsListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
Method customMethod = /* code to find my method */
Object[] params = /* this is where help is needed */
customMethod.invokeMethod(iTestResult.getInstance(), params);
}
问题是我该怎么做?
现在,TestNG提供了一个称为IDataProviderListener
的新侦听IDataProviderListener
,该侦听IDataProviderListener
在调用dataprovider之前和之后调用。 使用此方法,我们可以获取dataprovider方法并将其保存为静态方法(在所有配置和测试方法之前调用Dataproviders)。 稍后可以调用此dataprovider方法以在beforeInvocation()
方法中获取运行时Object []值。
这不是最干净的解决方案,但是它可以完成工作,并且可以作为进一步增强功能的良好起点。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.testng.IDataProviderListener;
import org.testng.IDataProviderMethod;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener2;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
public class CustomListen implements IInvokedMethodListener2, IDataProviderListener {
private static Method savedDataProviderMethod = null;
@Override
public void afterDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod testNGMethod, ITestContext context) {
System.out.println(">> afterDataProviderExecution start");
savedDataProviderMethod = dataProviderMethod.getMethod();
System.out.println("<< afterDataProviderExecution end");
}
@Override public void beforeDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod testNGMethod, ITestContext context) {}
@Override public void afterInvocation(IInvokedMethod method, ITestResult testResult) {}
@Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {}
@Override public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {}
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {
System.out.println(">> beforeInvocation start");
// Method customMethod = // code to find custtom method;
try {
Object[] params = (Object[]) savedDataProviderMethod.invoke(testResult.getInstance());
for (Object param : params) {
System.out.println("Dataprovider return data --" + param);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// customMethod.invokeMethod(testResult.getInstance(), params);
System.out.println("<< beforeInvocation end");
}
}
在以下测试类别上通过TestNG 6.14.3进行了测试
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ABCTest {
@DataProvider
public Object[] one() {
return new Object[] { 1 };
}
@BeforeMethod
public void setUp(Object[] params) {
System.out.println("setUp execution");
Assert.assertEquals(params[0], 1);
}
@Test(dataProvider = "one")
public void test(Integer intParam) {
System.out.println("test execution");
}
}
给定输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
>> afterDataProviderExecution start
<< afterDataProviderExecution end
>> beforeInvocation start
Dataprovider return data --1
<< beforeInvocation end
setUp execution
>> beforeInvocation start
Dataprovider return data --1
<< beforeInvocation end
test execution
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.