surefire并行threadCount被忽略(JUnit 4)

时间:2016-03-11 21:01:18

标签: junit4 maven-surefire-plugin parallel-testing

我已经看过很多关于与maven surefire并行运行JUnit测试的主题,但我没有看到处理这个特定问题的答案。

简而言之:测试方法是并行执行的(好消息),但是道具不会限制我的线程。最终目标是并行化我的Web驱动程序和appium运行,但我希望能够首先控制所有可能的线程。

下面的代码片段来自虚拟项目,仅用于演示。在apache documentation之后,它看起来并不比下面的更多。

的pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <parallel>methods</parallel>
                <threadCountMethods>2</threadCountMethods>
            </configuration>
        </plugin>
    </plugins>
</build>

ExampleTest.class

public class ExampleTest {

    @Test
    public void one() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void two() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void three() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void four() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void five() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void six() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void seven() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void eight() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void nine() throws InterruptedException {
        Thread.sleep(5000);
    }

    @Test
    public void ten() throws InterruptedException {
        Thread.sleep(5000);
    }
}

我希望使用提供的pom配置完全运行此类需要花费25秒以上的时间(10次睡眠,持续5秒,每次运行2次)。实际运行时间略多于5秒:

Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.022 sec - in ExampleTest

更改threadCountMethods prop对运行时没有影响(看起来所有方法并行,而不考虑threadCount prop)。

任何意见都将受到高度赞赏;谢谢!

1 个答案:

答案 0 :(得分:6)

因此,在进行了一些挖掘之后,我发现了一个名为perCoreThreadCount true的小财产here。如果未指定,则该prop默认为false。在我的机器上总共有8个核心,每个核心2个线程(来自问题中的原始pom配置)意味着最多16个样本测试可以在5秒内运行。在pom中将该prop和设置值添加到path可以解决所有问题!

相关问题