将带有 TestNG DataProvider parallel = true 的参数从 @Test 传递给 Listeners

Passing argument with TestNG DataProvider parallel = true from @Test to Listeners

我让 DataProvider 生成一些数据并将其提供给我的@Test

@DataProvider(name = "dp", parallel = true)
public Object[][] getBrowser(Method m) {

  Iterator < String > iterator = Constants.browsers.iterator();
  String browser = null;
  Object[][] data = new Object[2][1];

  for (int i = 0; i < 2; i++) {
    if (iterator.hasNext()) {
      browser = iterator.next();
      data[i][0] = browser;
    }
  }

  return data;

}

Constants.browsers 看起来像这样

public static final List<String> browsers = new ArrayList<>(
        Arrays.asList("Chrome","Edge"));

基本上只是生成 2 个数据“Chrome”和“Edge”,然后将其提供给我的 @Test,它看起来像这样

@Test(dataProviderClass = TestUtilities.class, dataProvider = "dp")
public void testing(String browser, ITestContext context) throws InterruptedException {

  ITestResult result = Reporter.getCurrentTestResult();
  result.getMethod().setDescription(browser);

  context.setAttribute("browser", browser);

  initiate(browser, Constants.baseURL); //Instantiate Chrome or Edge WebDriver and go to the URL

  //  Do the testing

  closeBrowser();
}

我已经设置了 TestNG 侦听器

public class myListeners extends TestBase implements ITestListener, ISuiteListener {

  @Override
  public void onTestSuccess(ITestResult result) {

    String methodName = result.getMethod().getMethodName();

    String browser = result.getMethod().getDescription();
    log.debug("ITestResult --> BROWSER is " + browser);

  }



  @Override
  public void onFinish(ITestContext context) {

    String usedBrowser = null;
    usedBrowser = (String) context.getAttribute("browser");
    log.debug("ITestContext --> Used Browser  : " + usedBrowser);

  }
}

@DataProvider(name = "dp", parallel = true) 开始,将同时有 2 个线程 运行ning。 Chrome浏览器和Edge浏览器。我可以看到它们同时启动并进行一些测试。测试完成后,两个浏览器都退出()。

我 运行 来自我的 testng.xml 看起来像这样

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="My Test Suite" parallel="tests">

  <listeners>
    <listener class-name="myListeners.myListeners"></listener>
  </listeners>

  <test name="myTest" preserve-order="true">
    <classes>
      <class name="testCases.testing" />
    </classes>
  </test>
</suite>

我的问题是听众只打印其中一个线程。 有时它打印

18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Chrome

18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Chrome

有时会打印

18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Edge

18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Edge

我的期望是两个参数都可以打印(记录),就像这样(不用担心时间戳)

18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Chrome

18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Edge

18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Chrome

18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Edge

我该怎么做?谢谢。

按照您在 ITestContext 中为 ITestResult 设置的属性进行设置

@Test(dataProviderClass = TestUtilities.class, dataProvider = "dp")
public void testing(String browser, ITestContext context) throws InterruptedException {

  ITestResult result = Reporter.getCurrentTestResult();
  // Changes here 
  result.setAttribute("browser", browser);

  context.setAttribute("browser", browser);

  initiate(browser, Constants.baseURL); //Instantiate Chrome or Edge WebDriver and go to the URL

  //  Do the testing

  closeBrowser();
}

@Override
  public void onTestSuccess(ITestResult result) {

    String methodName = result.getMethod().getMethodName();

    //String browser = result.getMethod().getDescription();
  // changes here
    log.debug("ITestResult --> BROWSER is " + (String) result.getAttribute("browser"));

  }

以下方法只会执行一次

  /**
   * Invoked after all the test methods belonging to the classes inside the <test> 
   tag have run
   * and all their Configuration methods have been called.
   *
   * @param context The test context
   */

    @Override
      public void onFinish(ITestContext context) {
    
        String usedBrowser = null;
        usedBrowser = (String) context.getAttribute("browser");
        log.debug("ITestContext --> Used Browser  : " + usedBrowser);
    
      }

你会得到输出 - 像这样

DEBUG TestBase -ITestResult --> BROWSER is Chrome

DEBUG TestBase -ITestResult --> BROWSER is Edge

DEBUG TestBase -ITestContext --> Used Browser  : Edge --- Note this--

On Finish 只会执行一次