Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run maven test in parallel without waiting for sibling module dependencies

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.

like image 977
Alex A Avatar asked Jul 28 '20 15:07

Alex A


People also ask

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

How do I run the Maven test clean?

To run all the unit tests with Maven run command $ mvn test or $ mvn clean test . If you use clean , all the resources and compiled java code generated by maven in target directory will be cleaned and run tests freshly.

Can you run tests in parallel in Maven?

Also, tests that mutate shared state are definitely not good candidates for running in parallel. 4. Test Execution in Multi-Module Maven Projects Till now, we've focused on running tests in parallel within a Maven module. But let's say we have multiple modules in a Maven project.

What happens when Maven compile all the modules one by one?

Here maven will first check that mentioned dependecies (with test classifier) are avilable or not then it checks the scope. As a result compiling all the modules one-by-one, skipping compilation of the test classes in them will give build failure, when maven will come to compile the module dependent-module1.

Is-T compatible with multi-module parallel Maven builds?

Regarding the compatibility with multi-module parallel maven builds via -T, the only limitation is that you can not use it together with forkCount=0.

How does Maven resolve dependencies from the same project?

This allows Maven to resolve dependencies on modules from the same multi-module project, regardless of the location of the starting POM. When Maven fails to find the root, it assumes that the starting POM is the root. For consistent behaviour, create a .mvn directory in the root directory of the project.


1 Answers

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.

like image 116
drekbour Avatar answered Sep 29 '22 17:09

drekbour