如何在iTestListener的方法"onTestStart"中获取测试数据(由dataprovider提供)

How to the get test data (provided by dataprovider) in the method "onTestStart" of iTestListener

所以我要解决的问题是:

我有测试 class 和 @test 方法 runtest 从数据提供者接收数据。在启动 运行 测试方法之前,我想根据测试数据 运行 测试将从数据提供者接收到的数据执行一些操作。

为此我查看了 iTestListener,它有一个方法 onTestStart 但我无法弄清楚如何获取 运行 实例的测试数据方法。

欢迎任何其他好的方法。

创建一个 class 扩展 TestListenerAdapter

 public class TestListener extends TestListenerAdapter {
    @Override
    public void onTestStart(ITestResult tr) {
       super.onTestStart(tr);
       Object[] params = tr.getParameters();
       String a = (String)params[0];
       int b = (int)params[1];
       //Add whatever you want to do before the test case starts
    }
 }

为您的测试添加注释class

@Listeners(ResultReporter.class)
public class CoreSingleApplicant1TestCase {

   @Test(dataprovider = "dataprovider",dataProviderClass = StaticProvider.class))
   public void runtest(String a, int b){
   }
}

数据提供者class

public class StaticProvider {
  @DataProvider(name = "create")
  public static Object[][] createData() {
    return new Object[][] {
      new Object[] { 
         {"String", 1},
         {"Integer",2} }
    }
  }
}