@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);
}
问题是我该怎么做?
答案 0 :(得分:0)
TestNG现在提供了一个名为IDataProviderListener
的新侦听器,它在调用dataprovider之前和之后调用。使用此方法,我们可以获取dataprovider方法并将其保存为静态方法(在所有配置和测试方法之前调用Dataproviders)。稍后可以调用此数据提供程序方法以获取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.3import 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