java JUnit 和 Surefire 并行测试 - ForkCount 和 ThreadCount

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32987998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:02:25  来源:igfitidea点击:

JUnit and Surefire Parallel Tests - ForkCount & ThreadCount

javaseleniumjunitparallel-processingsurefire

提问by userMod2

I'm running Selenium tests on the Selenium Grid using the Surefire Plugin to execute tests. In terms of my test breakdown I have several classes, some of which have 1 test in there and some more than one test.

我正在使用 Surefire 插件在 Selenium Grid 上运行 Selenium 测试来执行测试。就我的测试细分而言,我有几个课程,其中一些课程有 1 个测试,有些课程不止一个。

So on my Grid i have 30 chrome web drivers and I want to execute all tests within all classes in parallel.

所以在我的 Grid 上,我有 30 个 chrome web 驱动程序,我想并行执行所有类中的所有测试。

I've read how to do this using the parallelparameter which i have set as:

我已经阅读了如何使用parallel我设置的参数来做到这一点:

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <includes>
                        <include>${testSuite}</include>
                    </includes>
                    <parallel>all</parallel>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <perCoreThreadCount>false</perCoreThreadCount>
                    <threadCount>20</threadCount>
                    <browser>${browser_type}</browser>
                </configuration>
            </plugin>

However this doesnt seem to fill all the Chrome web drivers I have available.

然而,这似乎并没有填满我可用的所有 Chrome 网络驱动程序。

If i then use forkCount, like:

如果我然后使用forkCount,例如:

<forkCount>20</forkCount>
<reuseForks>true</reuseForks>

Then when the test execution first starts, all web drivers are filled however it quickly starts dropping and behaving one at a time.

然后当测试执行第一次开始时,所有的 web 驱动程序都被填满了,但是它很快开始下降并一次运行一个。

So my questions:

所以我的问题:

  • Is there a relationship between forkCount and threadCount
  • Is there anything additional I need to do to really get this running in parallel?
  • forkCount 和 threadCount 有关系吗
  • 我需要做任何其他事情才能真正并行运行它吗?

Thanks.

谢谢。

回答by ursa

You have to provide explicit junit test provider:

您必须提供显式的 junit 测试提供程序:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <parallel>all</parallel>
        <useUnlimitedThreads>true</useUnlimitedThreads>
        <useSystemClassLoader>false</useSystemClassLoader>
        <includes>
            <include>${testSuite}</include>
        </includes>
        <systemPropertyVariables>
            <browser>${browser_type}</browser>
        </systemPropertyVariables>
     </configuration>
</plugin>

And you should use JUnit 4.7+ as older versions does not work with parallel testing correctly.

并且您应该使用 JUnit 4.7+,因为旧版本无法正确处理并行测试。

Also you can omit fork-related parameters if your tests do NOT affect JVM runtime (usually it's not the case).

如果您的测试不影响 JVM 运行时(通常情况并非如此),您也可以省略与 fork 相关的参数。

Or migrate your tests to TestNG - it is more elegant framework and it works with parallel testing much better, then JUnit (imo).

或者将您的测试迁移到 TestNG - 它是更优雅的框架,并且可以更好地与并行测试一起使用,然后是 JUnit (imo)。

回答by Federico Piazza

There are so many configuration for running test in parallel.

有很多用于并行运行测试的配置。

According to the documentation:

根据文档:

forkCount

叉数

The parameter forkCountdefines the maximum number of JVM processes that Surefire will spawn concurrently to execute the tests. It supports the same syntax as -Tin maven-core: if you terminate the value with a C, that value will be multiplied with the number of available CPU cores in your system. For example forkCount=2.5Con a Quad-Core system will result in forking up to ten concurrent JVM processes that execute tests.

...

The default setting is forkCount=1/reuseForks=true, which means that Surefire creates one new JVM process to execute all tests in one maven module.

该参数forkCount定义了 Surefire 将同时产生以执行测试的最大 JVM 进程数。它支持与-Tmaven-core相同的语法:如果用 a 终止值C,则该值将乘以系统中可用的 CPU 内核数。例如forkCount=2.5C,在四核系统上将导致最多 10 个执行测试的并发 JVM 进程分叉。

...

默认设置是forkCount=1/ reuseForks=true,这意味着 Surefire 创建一个新的 JVM 进程来执行一个 maven 模块中的所有测试。

threadCount

线程数

When using reuseForks=trueand a forkCountvalue larger than one, test classes are handed over to the forked process one-by-one. Thus, parallel=classeswould not change anything. However, you can use parallel=methods: classes are executed in forkCountconcurrent processes, each of the processes can then use threadCountthreads to execute the methods of one class in parallel.

当使用reuseForks=trueforkCount大于1的值时,测试类一一交给fork进程。因此,parallel=classes不会改变任何东西。但是,您可以使用parallel=methods:类在forkCount并发进程中执行,然后每个进程可以使用threadCount线程并行执行一个类的方法。

As fair as I understand, threadCountis like a sub threading for each fork.

据我所知,threadCount就像每个叉子的子线程一样。

You can tweak these params to improve your test performance, for instance you can have:

您可以调整这些参数以提高测试性能,例如您可以:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
         <includes>
              <include>${testSuite}</include>
         </includes>
         <parallel>all</parallel>
         <useSystemClassLoader>false</useSystemClassLoader>
         <perCoreThreadCount>false</perCoreThreadCount>
         <forkCount>2.0C</forkCount>
         <reuseForks>true</reuseForks>
         <threadCount>20</threadCount>
         <browser>${browser_type}</browser>
    </configuration>
</plugin>

You can find more details in its site:

您可以在其网站上找到更多详细信息:

https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html