surefire并行threadCount被忽略(JUnit 4)

pap*_*y-o 4 junit4 parallel-testing maven-surefire-plugin

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

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

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

的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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这个类的完整运行与提供的pom配置需要花费超过25秒(10次睡眠,持续5秒,每次运行两次).实际运行时间略大于5秒:

Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.022 sec - in ExampleTest
Run Code Online (Sandbox Code Playgroud)

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

任何投入将不胜感激; 谢谢!

pap*_*y-o 7

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