Surefire:suitesAndClasses 和 类 之间的区别

Surefire: Difference between suitesAndClasses and classes

当我运行在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

执行

正常顺序执行

文件 pom.xml 中没有配置 maven-surefire-plugin 或至少没有 <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 可以并行执行。