【问题标题】:TestNG parallel runs with a single threaded data providerTestNG 使用单线程数据提供程序并行运行
【发布时间】:2013-03-05 19:40:18
【问题描述】:

我想利用来自数据提供者的 1 个调用计数的并行测试,我该如何实现?

下面是一些示例代码:

public class Example {


  @Test(dataProvider = "provider", threadPoolSize = 20)
  public void test(final TestData td) {
    System.out.println(td);
    Assert.assertTrue(td.i >= 0);
  }

  @DataProvider
  public Object[][] provider() {
    final Random r = new Random(System.currentTimeMillis());
    final Object[][] data = new TestData[5][1];
    for (int i = 0; i < 5; i++) {
      data[i][0] = new TestData(r.nextInt(5), i, "test string");
    }
    return data;
  }

  private class TestData {
    public int random;
    public int creationNum;
    public String s;

    public TestData(final int random, final int creationNum, final String s) {
      this.random = random;
      this.creationNum = creationNum;
      this.s = s;
    }

    @Override
    public String toString() {
      final List<String> list = new ArrayList<>();
      for (int j = 0; j < i; j++) {
        list.add(s);
      }
      return "I = " + i + ", Creation Num: " + creationNum + ", " + StringUtils.join(list, ", ");
    }
  }
}

所以当我运行这个测试时,我希望看到 creationNumber 以随机顺序出现。问题是如果未提供 invocationCount,则忽略 threadPoolSize。如果我提供,我将看到用于多个测试的相同 TestData 实例——我不希望发生这种情况。有没有办法让我以线程方式运行此测试而无需多次测试相同的数据?

【问题讨论】:

    标签: java multithreading testng


    【解决方案1】:

    你可以让@DataProvider(parallel = true) 并使用您的 dataproviderthreadcount 来控制您要生成的并行线程数。

    【讨论】:

    • 啊,做到了。我误解了那个标志。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多