Nathan Nathan - 1 month ago 19x
Java Question

How to Run Unit Test in Parallel Across All Modules Using Maven?

I have a multi-module Maven project. In the root pom.xml, I have added the following...


The command
mvn test
will cause each module to be built (if needed) and then the tests for that module will be run in parallel. Once all of the tests are done, Maven starts working on the next module. Maven will work on each module sequentially.

Inside, Eclipse IDE I can run TestNG against a suite called
. This file specifies that all packages should be run in parallel. This causes TestNG to run all test methods across all modules in parallel. This is exactly what I want to do but I am not sure how. A benefit of this is that all of the tests are run in 1 process and can share state between them. This state sharing speeds up the tests quite a lot. Note: I thought about using ant to execute a command but then I wasn't sure how to specify a classpath with the dependencies from the Maven repository.

mvn -T 8 test
will allow for parallelism but because of dependencies between the modules I don't get all of the tests run in a parallel. I would be okay with running
mvn compile test-compile -DskipTests=true
first and then running
mvn -T 8 test
if this would run all of the tests in parallel.

Edit: The tests run are a mixture of unit and integration tests. With the option
, I can pick which set of tests are executed.


In the last module to be built excluding Root, I added the following to the pom.xml.


Then to run the tests, I use the following command:

mvn -DskipTests=true "-Dexec.args=testng.xml" test

The -DskipTests=true tells Maven to not execute the tests but the test phase tells Maven to build everything for the tests and run the above plugin. The "-Dexec.args=testng.xml" is an argument to TestNG in the above plugin configuration. Here's the contents of testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "" >

<suite name="All" verbose="1" parallel="methods" thread-count="100">

  <test name="All">
      <package name=".*"/>


The parallel="methods" attribute allows TestNG to run in parallel each test method. The thread-count="100" attribute limits TestNG to running 100 threads. The package name=".*" attribute tells TestNG to run all tests in all packages.