surefire-plugin 不遵守 threadCount 参数

surefire-plugin does not respect threadCount parameter

几天以来,我试图查看我的配置错误在哪里 运行 并行我的 Selenium 测试。

我有一个带有 2 个节点的 Selenium 网格。 在我的 pom.xml 中,我将 surefire 设置为 运行 2 by 2 我对特定类别的测试方法,然后是其他测试。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <id>default-test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <parallel>methods</parallel>
                        <perCoreThreadCount>false</perCoreThreadCount>

                        <threadCount>2</threadCount>
                        <reuseForks>false</reuseForks>
                        <groups>
                            com.something.categories.Safe,
                            com.something.categories.Parallel
                        </groups>
                    </configuration>
                </execution>
                <execution>
                    <id>no-safe</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <excludedGroups>
                            com.something.categories.Safe,
                            com.something.Parallel
                        </excludedGroups>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

当我启动我的测试时 mvn clean test -Dtest='TestAwesome' TestAwesome 中包含的所有测试同时启动(我看到超过 2 个浏览器打开),所以不尊重我的 threadCount 值。

我错过了什么?

回答后的版本 这是我的部分 pom.xml 来解决我的问题

<profiles>
    <profile>
        <id>selenium-tests</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <parallel>all</parallel>
                        <threadCount>${threads}</threadCount>
                        <perCoreThreadCount>false</perCoreThreadCount>
                        <useUnlimitedThreads>true</useUnlimitedThreads>
                        <systemProperties>
                            <browser>${browser}</browser>
                            <screenshotDirectory>${project.build.directory}/screenshots</screenshotDirectory>
                            <gridURL>${seleniumGridURL}</gridURL>
                            <env>${env}</env>
                        </systemProperties>
                        <groups>${groups}</groups>
                        <excludedGroups>${excludedGroups}</excludedGroups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

由于您使用的是足够现代的 surefire 版本,您可能想尝试将 threadCountMethods 参数而不是 threadCount 与 useUnlimitedThreads[=22= 结合使用] = 正确,尽管它看起来违反直觉。

surefire jUnit examples:

As of Surefire 2.7, no additional dependencies are needed to use the full set of options with parallel. As of Surefire 2.16, new thread-count attributes are introduced, namely threadCountSuites, threadCountClasses and threadCountMethods.

Fork options and parallel execution:

As an example with an unlimited number of threads, there is maximum of three concurrent threads to execute suites: parallel = all, useUnlimitedThreads = true, threadCountSuites = 3.