junit 如何在并行执行中运行X次相同的测试?

c90pui9n  于 2023-05-17  发布在  其他
关注(0)|答案(2)|浏览(208)

我有个简单的测试

@Test
public void searchInGoogle() {
    final String searchKey = "TestNG";
    System.out.println("Search " + searchKey + " in google");
    driver.navigate().to("http://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    System.out.println("Enter " + searchKey);
    element.sendKeys(searchKey);
    System.out.println("submit");
    element.submit();
    System.out.println("Got " + searchKey + " results");
}

我想并行运行10次,这意味着10个chrome窗口将并行打开并执行相同的测试。
请帮帮忙,我见过类似的事情,但不完全是这样。
谢谢!

ccgok5k5

ccgok5k51#

在JUnit 5中,您可以使用@RepeatedTest多次运行单个测试用例。
虽然目前仍是一个实验性的特性,但您也可以指定测试应该并行执行,方法是将junit.jupiter.execution.parallel.enabled设置为true,作为JUnit 5配置设置的一部分。
然后,您的代码将如下所示:

@Execution(CONCURRENT)
class GoogleSearchTest {

  import ...

  @RepeatedTest(10)
  public void searchInGoogle() {
    ...
  }
}
7ajki6be

7ajki6be2#

这是旧的,所以我相信你现在已经找到了一个解决方案,但是你能不能把测试逻辑移到一个方法中去:

IntStream.range(1, 10).parallel().forEach(this::myTestMethod);

相关问题