Run maven test in parallel without waiting for sibling module dependencies
Asked Answered
R

2

7

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.

Rebuttal answered 28/7, 2020 at 15:16 Comment(5)
Why do you build your modules first before you run the tests? Why not just build everything with mvn clean install?Ticker
@JFabianMeier I do something on the lines of a clean install but skip tests initially so that I may run the tests in parallel with other code analysis tools afterwords.Rebuttal
If they are highly dependent on each other, either refactor the bits that are dependent or remove the dependencies if the are unsued. As you already told, aiming to parallize a reactor build which is essentially a List instead of a somewhat balanced Tree does not work.Counterattack
That's a feature not a bug. If module B fails its tests, then Module A will probably fail its tests as well because module B isn't functional. You will have extra noise in your test results that could make it harder for you or your team to identify the true issue.Effuse
@JohnBrandli completely understandable, and I'm sure useful in many scenarios - but is it also something one could disable? :)Rebuttal
R
4

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 (<module>-tests).

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

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

Redraft answered 3/8, 2020 at 13:4 Comment(0)
T
0

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
Tumefaction answered 30/7, 2020 at 21:54 Comment(3)
Well the entire point is that of not skipping tests :) I want to run them but without having to wait for dependant modules to run their tests first. Maybe I am missinterpreting your answer though. If I do this, will my tests be run in a parallel way that does not care about inter-module dependencies?Rebuttal
Your requirement is o skip compile and run tests in parallel. Whether all 8 threads work on the same module or different module does not matter as the work done by the 8 threads is sameTumefaction
Well from my understanding (and testing) the -T parameter configuration described above will never build/test a single module on 8 threads, it will allow building independent modules / modules with already fullfiled dependencies, 1 per thread. As per: cwiki.apache.org/confluence/display/MAVEN/… where it states: This build-mode analyzes your project's dependency graph and schedules modules that can be built in parallel according to the dependency graph of your project.Rebuttal

© 2022 - 2024 — McMap. All rights reserved.