你考虑过使用 InvokedMethodListener2 吗?
public Class BaseTestNg {
ITestContext ctx;
public ITestContext getContext(){
return this.ctx;
}
@BeforeClass
public void setContext( final ITestContext ctx ) {
this.ctx = ctx;
}
}
public Class SampleTest extends BaseTestNg {
@Test
public Market testMarketGeneration(){
//generate market
this.getContext().setAttribute("market", someMarket);
//returning generated market.
return someMarket;
}
}
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener2;
import org.testng.ITestContext;
import org.testng.ITestResult;
public class InvokedMethodListener2Impl implements IInvokedMethodListener2 {
@Override
public void afterInvocation( final IInvokedMethod method, final ITestResult testResult ) {
// TODO Auto-generated method stub
}
@Override
public void afterInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {
//read object from context
//Market = (Market) context.getAttribute("market");
// testResult.setStatus( ITestResult.FAILURE );
}
@Override
public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult ) {
// TODO Auto-generated method stub
}
@Override
public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {
// TODO Auto-generated method stub
}
}