Backend Development 10 min read

Optimizing Maven Surefire Parallel Execution for Large-Scale Unit Tests

The Taobao User Operations Platform team reduced CI unit‑test time from fifty minutes to ten by configuring Maven Surefire to reuse forks, use a fork count of twice the CPU cores, and run methods in parallel with an appropriate thread count, achieving faster builds while keeping failure rates low.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Optimizing Maven Surefire Parallel Execution for Large-Scale Unit Tests

This article shares the experience of the Taobao User Operations Platform team in improving unit‑test efficiency on their CI pipeline (aone). It describes the problem of test failures caused by concurrency and the long execution time when test cases scale up.

The testing stack consists of three core tools: JUnit for writing tests, JaCoCo for code‑coverage measurement, and the Maven Surefire Plugin for executing tests in the pipeline.

Phase 1 – Test‑case accumulation : As the number of test cases grew, random failures appeared in the pipeline due to race conditions when tests ran concurrently. The team first limited concurrency by disabling fork reuse and setting forkCount to 1.

org.apache.maven.plugins maven-surefire-plugin 2.16 false 1

Phase 2 – Large test suite : With many test cases the total runtime reached ~50 minutes, severely impacting developer productivity. To speed up execution while keeping failure rates low, the team explored parallel execution options.

The Surefire plugin offers several parameters:

parallel – execution mode (methods, classes, both, suites)

threadCount – number of threads

useUnlimitedThreads – no thread‑limit

perCoreThreadCount – threads per CPU core

parallelTestsTimeoutInSeconds – timeout

When parallel is enabled, either useUnlimitedThreads or threadCount must be set, otherwise Surefire throws an error.

After testing several combinations, the optimal configuration was found to be reuseForks=true and forkCount=2C (twice the number of CPU cores). This reduced the average run time to about 10 minutes with a low error probability.

org.apache.maven.plugins maven-surefire-plugin 3.0.0-M7 methods 10

Additional tips:

Enable Maven parallel builds (e.g., mvn -T 1C clean test ) only when the test suite is distributed across modules.

Adjust concurrency per project: some projects benefit from high parallelism, others prioritize stability.

In summary, understanding Surefire’s concurrency model and tailoring fork and parallel settings to the specific test workload can dramatically improve CI efficiency without sacrificing reliability.

JavaMavenJunitCIParallelTestingSurefireTestOptimization
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.