surefire-plugin does not respect threadCount parameter

tet*_*nne 5 junit selenium maven maven-surefire-plugin selenium-webdriver

Since several days, I tried to see where is my mistake in my configuration to run in parallel my Selenium tests.

I have a Selenium Grid with 2 nodes. In my pom.xml, I have set surefire to run 2 by 2 the methods of my tests with a particular category then other tests.

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

When I launch my test mvn clean test -Dtest='TestAwesome' all the tests contains in TestAwesome are launched in the same time (I see more than 2 browsers opended), and so does not respect my threadCount value.

I'm missing something?

Edition after answer Here my partial pom.xml to solve my issue

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

Car*_*ten 3

由于您使用的是 Surefire 的现代版本,您可能想尝试将threadCountMethods参数而不是 threadCount 与useUnlimitedThreads = true 结合使用,即使它看起来违反直觉。

Surefire jUnit 示例

从 Surefire 2.7 开始,不需要额外的依赖项即可使用完整的并行选项集。从 Surefire 2.16 开始,引入了新的线程计数属性,即 threadCountSuites、threadCountClasses 和threadCountMethods

分叉选项和并行执行

作为线程数量不受限制的示例,最多有 3 个并发线程可以执行套件:parallel = all、useUnlimitedThreads = true、threadCountSuites = 3。