我想利用来自数据提供者的 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 实例——我不希望发生这种情况。有没有办法让我以线程方式运行此测试而无需多次测试相同的数据?