gpt4 book ai didi

java - Surefire:suitesAndClasses和classes之间的区别

转载 作者:行者123 更新时间:2023-12-01 11:34:57 24 4
gpt4 key购买 nike

当我在 Surefire 中运行测试时,有什么区别

<parallel>suitesAndClasses</parallel>

<parallel>classes</parallel>

第一个示例似乎并行运行我的测试类,而第二个示例则不然。

最佳答案

请在下面找到一个简单的示例来验证 <parallel>classes</parallel>按预期工作。支票<parallel>suitesAndClasses</parallel>它将并行执行 JUnit 测试套件和普通 JUnit 测试类。

测试类

public class ParallelOneTest {

@Test
public void testOne() throws InterruptedException {
System.out.println("ParallelOneTest.testOne()");
TimeUnit.SECONDS.sleep(5);
}

@Test
public void testTwo() throws InterruptedException {
System.out.println("ParallelOneTest.testTwo()");
TimeUnit.SECONDS.sleep(5);
}

@Test
public void testThree() throws InterruptedException {
System.out.println("ParallelOneTest.testThree()");
TimeUnit.SECONDS.sleep(5);
}
}

.

public class ParallelTwoTest {

@Test
public void testOne() throws InterruptedException {
System.out.println("ParallelTwoTest.testOne()");
TimeUnit.SECONDS.sleep(5);
}

@Test
public void testTwo() throws InterruptedException {
System.out.println("ParallelTwoTest.testTwo()");
TimeUnit.SECONDS.sleep(5);
}

@Test
public void testThree() throws InterruptedException {
System.out.println("ParallelTwoTest.testThree()");
TimeUnit.SECONDS.sleep(5);
}
}

以下所有描述的测试场景均按 mvn clean compile test 执行。

正常顺序执行

要么没有配置maven-surefire-plugin在文件pom.xml中或者至少没有<parallel>...</parallel>条款。

顺序输出(剥离)

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.07 sec
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.008 sec
...
[INFO] -------------------
[INFO] BUILD SUCCESS
[INFO] -------------------
[INFO] Total time: 31.919s

并行执行

pom.xml

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<parallel>classes</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
</plugins>
</build>

并行输出(剥离)

[INFO] parallel='classes', perCoreThreadCount=true, threadCount=2, ...
...
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.023 sec - in sub.optimal.mavenscratch.ParallelTwoTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.023 sec - in sub.optimal.mavenscratch.ParallelOneTest
...
[INFO] -------------------
[INFO] BUILD SUCCESS
[INFO] -------------------
[INFO] Total time: 16.770s

正如您在第二个示例中看到的(基于总时间),两个测试类都是并行执行的。

对于并行选项 suitesAndClasses您可以配置多个类 <threadCountClasses>和测试套件threadCountSuites可以并行执行。

关于java - Surefire:suitesAndClasses和classes之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com