Explain Codes LogoExplain Codes Logo

Making Maven run all tests, even when some fail

maven
test-failure
surefire-plugin
maven-configuration
Nikita BarsukovbyNikita Barsukov·Aug 27, 2024
TLDR

To make sure Maven proceeds with test executions even after encountering a failure, add the -DtestFailureIgnore=true flag to your command:

mvn test -DtestFailureIgnore=true

This command instructs Maven to ignore test failures and continue executing the remaining tests, providing a more comprehensive view of your project's health.

Multi-module projects: going full steam ahead

In a multi-module project, a test failure in one module might halt the construction of dependent modules. However, Maven gives you a couple of options to avoid this scenario and carry on with the build:

mvn clean install -fae # --fail-at-end mvn clean install -fn # --fail-never
  • The -fae or --fail-at-end flag instructs Maven to carry on with the build, reporting the failures once all modules are built.
  • The -fn or --fail-never flag tells Maven to never fail the build regardless of what happens, like a bulldozer that stops for nothing!

Tame failing tests with Surefire configurations

To handle test failures more elegantly, use the Surefire plugin in your project's pom.xml file:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <!-- upgrade me if needed 😉--> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin>

This configuration at the root pom.xml ensures that all submodules follow the same test failure settings. Sharing is caring, after all!

Nested project builds: Every test counts

In a nested project build scenario, where the build crosses several nested projects having multitude of tests, use the -fn (--fail-never) switch:

mvn -fn test

This command makes Maven run to completion, testing all nested projects, regardless of earlier failures. It's like a marathon runner refusing to quit!

Balancing comprehensive tests and build success

Using -fn or -fae flags and the Surefire plugin, you can strike the right balance between having a comprehensive test suite and a successful build status. This approach prevents one failed test from halting your entire build and ensures that you spot all potential issues in your project's test cycle.

Configuration tips and common pitfalls

Here are a few recommendations and caveats for setting up Maven:

  • Use the -Dmaven.test.failure.ignore=true command line flag when you want to temporarily bypass test failure settings without modifying the pom.xml.
  • Be mindful that using <testFailureIgnore> might have its limitations in a multi-module project, especially if modules depend on each other.

Always refer back to the Maven Embedder documentation for a wider array of options and a deeper understanding of handling these configurations in complex builds.