简体   繁体   English

Maven Surefire插件+并行构建

[英]Maven Surefire plugin + parallel build

In our fairly large project (around 600 maven modules) we've decided to try maven's parallel build capability. 在我们相当大的项目(大约600个maven模块)中,我们决定尝试使用maven的并行构建功能。 So I've installed maven 3.3 and tried to run unit tests. 因此,我已经安装了maven 3.3,并尝试运行单元测试。

I've figured out that sometimes our unit tests fail. 我发现有时候我们的单元测试会失败。 If I run the module 'alone' it works, and of course if we run maven 'sequentially' (no -T option) it also works. 如果我“单独”运行模块,则可以使用,当然,如果我们“顺序”运行maven(无-T选项),则也可以使用。

So, I think that its due to the fact that sometimes tests interfere, probably some static code or shared instances cause these failures. 因此,我认为这是由于有时测试会干扰,可能是某些静态代码或共享实例导致了这些失败。

My question is, what is the optimal way to run the unit tests of my project? 我的问题是,运行项目的单元测试的最佳方法是什么? I'm aware of option to spawn JVM upon each test running, but since we have thousands of unit tests I'm afraid such a build would last forever :) 我知道可以在每次测试运行时生成JVM的选项,但是由于我们有成千上万的单元测试,所以我担心这样的构建将永远持续下去:)

AFAIK there are 3 possible solutions: AFAIK有3种可能的解决方案:

  1. Just skip the tests, compile/package/install everything in parallel. 只需跳过测试,并行编译/打包/安装所有内容。 And then run tests separately. 然后分别运行测试。 This approach can be feasible for jenkins, but I can be a hassle for developers who are just accustomed to run mvn install 这种方法对于jenkins来说是可行的,但是对于那些习惯于运行mvn install的开发人员来说,我可能会很麻烦。

  2. Somehow extend surefire plugin that it would automatically rerun N times the unit test if it fails. 以某种方式扩展了surefire插件,使其在失败时将自动重新运行N次单元测试。 If the test fails, say 50% of times - its really unstable and should cause the whole build failure. 如果测试失败,请说50%的次数-这确实很不稳定,应该会导致整个构建失败。 I wouldn't like to use a @RunWith annotation because of two reasons: a. 由于两个原因,我不希望使用@RunWith注释: There are just to many tests to update with this annotation and we don't have a base class or something. 仅有大量测试可以使用此批注进行更新,而我们没有基类或其他东西。 b. Some tests already contain this annotation (for power mock for example, or junit rules) 一些测试已经包含此注释(例如,用于电源模拟或junit规则)

  3. Somehow extend surefire plugin to run tests in different classloader. 以某种方式扩展surefire插件以在不同的类加载器中运行测试。 This solution has come to my mind due to the fact that maven seems to run multiple modules with the same classloader (I've just put 2 tests that print the address of classloader in two different submodules and ensured that the classloader is the same, the addresses are identical). 由于Maven似乎使用同一个类加载器运行多个模块(我刚刚在两个不同的子模块中放置了两个打印类加载器地址的测试,并确保类加载器相同,所以我想到了该解决方案)。地址相同)。 This solution looks interesting but relatively complicated. 该解决方案看起来很有趣,但是相对复杂。

Before diving to any of these solution and talking to my managers, I would appreciate if someone could comment on one of these solutions/provide another one. 在探讨任何一种解决方案并与我的经理交谈之前,我希望能有人对这些解决方案之一发表评论/提供另一种解决方案。 I Just don't want to reinvent the wheel and hopefully save some time :) 我只是不想重新发明轮子,希望节省一些时间:)

Thanks a lot in advance 在此先多谢

How about running each test class in a separate JVM? 如何在单独的JVM中运行每个测试类? (And possibly reuse the forked JVMs in other tests). (并可能在其他测试中重用分叉的JVM)。

Honestly, it looks like you didn't give it a shoot. 老实说,您似乎没有为此开枪。 Check out the doc 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

Just a copy paste of the example: 只是示例的复制粘贴:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19</version>
  <configuration>
    <forkCount>3</forkCount>
    <reuseForks>true</reuseForks>
    <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
    <systemPropertyVariables>
        <databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
    </systemPropertyVariables>
    <workingDirectory>FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
  </configuration>
</plugin>

By the way, surefire can re-run failing tests: https://maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html . 顺便说一句,surefire可以重新运行失败的测试: https : //maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html I'm just afraid you won't be able to set a threshold. 我只是担心您将无法设置阈值。

Also not that forking can make debugging (eg, attaching a debugger) quite difficult. 同样不是说分叉会使调试(例如,附加调试器)变得非常困难。

From my experience, not all the surefire settings are compatible with forking so you may need to go step by step while tuning the configuration to see what is actually working and what is not. 根据我的经验,并非所有的surefire设置都与fork兼容,因此您可能需要逐步调整配置以查看实际的工作方式和无效的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM