Run maven test in parallel without waiting for sibling module dependencies

1.2k views Asked by At

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.

2

There are 2 answers

3
Kavithakaran Kanapathippillai On

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
0
drekbour On

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.