TestNG parallel runs with a single threaded data p

2019-09-18 11:36发布

问题:

I'd like to utilize parallel tests with 1 invocation count from a data provider, how can I achieve this?

Here is some example code:

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, ", ");
    }
  }
}

So when I run this test, I would like to see the creationNumber come out in a random order. The problem is the threadPoolSize is ignored if an invocationCount is not provided. If I provide that, I will see the same TestData instances utilized for multiple tests -- which I do not want to have happen. Is there a way for me to have this test run in a threaded manner without testing the same data multiple times?

回答1:

You can make @DataProvider(parallel = true) and use your dataproviderthreadcount to control the number of parallel threads you want to spawn.