Surefire:suitesAndClasses和类之间的区别

时间:2015-05-07 10:08:11

标签: java surefire

当我在Surefire中运行测试时,

之间有什么区别
<parallel>suitesAndClasses</parallel>

<parallel>classes</parallel>

第一个例子似乎并行运行我的测试类,而第二个例子没有。

1 个答案:

答案 0 :(得分:1)

请在下面找到一个简单示例,以验证<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