4

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

简而言之:测试方法是并行执行的(那里有好消息),但 prop 并不能限制我的线程。最终目标是并行化我的 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>

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 秒,一次运行两个)。实际运行时间略多于 5 秒:

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

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

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

4

1 回答 1

7

perCoreThreadCount因此,经过更多挖掘,我发现了一个名为found here的小属性。如果未指定,则该道具默认为true. 我的机器上共有 8 个内核,每个内核 2 个线程(来自问题中的原始 pom 配置)意味着最多 16 个样本测试可以在 5 秒内运行。false在 pom 中添加该道具和设置值解决了所有问题!

于 2016-03-11T22:41:03.490 回答