在不等待同级模块依赖的情况下并行运行Maven测试。

huangapple go评论75阅读模式
英文:

Run maven test in parallel without waiting for sibling module dependencies

问题

我有一个多模块的Maven项目,这些模块之间有相当多的依赖关系。我尝试过并行运行我的测试,但由于这些模块的设计不够完善,彼此高度依赖,基本上我没有时间上的收益,因为它们最终仍然按顺序执行。

现在,在一起运行所有测试之前,我有一个阶段在其中构建项目,这样我就可以将其他静态分析工具与我的测试并行应用。

我的问题是:鉴于我的模块已经被编译,我是否可以告诉Maven使用这些预编译的类并且不等待依赖模块先运行它们的测试呢?
例如,当前如果我有一个依赖于模块B的模块A,模块B会在A开始之前执行其所有测试。由于我已经构建了A和B,我没有必要保持这种限制。

我当前运行测试的方式类似于:
mvn -f project-parent/pom.xml surefire:test,其中project-parent是一个包含所有其他模块的父模块。出于简洁起见,我省略了配置文件和其他参数。

谢谢!

英文:

I have a multi-module maven project in which the modules have quite a few dependencies between them. I have tried running my tests in parallel, but since the modules are not very well thought off and are highly dependent on each other, I basically have no time gain, since they still end up executing sequentially.

Now, before running the tests altogether I have a phase where I build the project so that I may apply other static analysis tools in parallel to my testing.

My question is: Given that my modules are already compiled, can I not tell maven to run tests in parallel using these precompiled classes and not wait for dependant modules to run their tests first?
E.g. currently if I have a module A depending on module B, module B would execute all its tests first before A could start. Since I have A and B already built, I have no need to keep this limitation.

The way I run tests currently is something like:
mvn -f project-parent/pom.xml surefire:test where project parent is a module parenting all my other modules. I have ommited profiles and other parameters for brevity.

Thanks!

Edit: I am trying to avoid class/suite level parallelization at this point using junit or surefire and would just like to test modules in a parallel way.

答案1

得分: 4

Child 测试可能会通过,但 Parent 测试会失败。这可能是因为 Child 需要 Parent,但是大多数测试在假设其正常工作的情况下对 Parent 进行了 mock!如果这种情况发生在并行且不按顺序的情况下,我认为 Maven 不会知道该怎么做。你希望 Maven 可以成为整个 CI 流水线,但实际上它只是一个构建工具。

然而:

如果你的测试足够慢,以至于你提出了这个 Stack Overflow 的问题,那么它们可能是可以提取到新模块(<module>-tests)中的集成测试。

A -> A-tests
A -> B
     B -> B-tests
     B -> C
          C -> C-tests

这意味着 A、B 和 C 的 src/test/* 可以快速完成并且可以像“正常情况下”一样运行,而较慢的 <module>-tests 之间彼此不依赖,这意味着 Maven 可以仅使用 mvn test -TC 并行运行它们。

英文:

Child tests can pass but Parent tests fail. This might be because Child requires Parent but most of its tests mock Parent on the assumption it works! If this happens in parallel and out of sequence, I don't think Maven would know what to do. You are hoping that Maven can be an entire CI pipeline when really it's just a build tool.

However:

If your tests are slow enough for you to raise this SO question, then they might be integration tests that can be extracted into new modules (&lt;module&gt;-tests).

A -&gt; A-tests
A -&gt; B
     B -&gt; B-tests
     B -&gt; C
          C -&gt; C-tests

This means the A, B and C src/test/* complete quickly and can be run "as normal" and the slower &lt;module&gt;-tests do not depend on each other meaning Maven can parallelise them with nothing more than mvn test -TC.

答案2

得分: 0

  • <a href="https://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html">跳过编译测试</a>

> 如果绝对必要,您还可以使用 maven.test.skip 属性来跳过编译测试。maven.test.skip 属性会被 SurefireFailsafe 以及 Compiler 插件所遵守。

   mvn -T 8 -f project-parent/pom.xml surefire:test -Dmaven.test.skip=true
英文:
  • <a href="https://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html">Skip compiling the tests</a>

> If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

   mvn -T 8 -f project-parent/pom.xml surefire:test -Dmaven.test.skip=true

huangapple
  • 本文由 发表于 2020年7月28日 23:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63137469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定