Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

Skipping Tests with Maven

$
0
0

1. Introduction

Skipping tests is often a bad idea. However, there are some situations where it could be useful — maybe when we’re developing new code and want to run intermediate builds in which the tests are not passing or compiling. In these kind of situations only, we might skip the tests to avoid the overhead of compiling and running them. However, consider that not running tests can lead to bad coding practices.

In this quick tutorial, we’ll explore all the possible commands and options to skip tests using Maven.

2. Maven Lifecycle

Before getting into the details of how to skip tests, we must understand when tests are compiled or run. In the article about Maven goals and phases, we go deeper into the concept of the Maven lifecycle, but for the purpose of this article, it’s important to know that Maven can:

  1. Ignore tests
  2. Compile tests
  3. Run tests

In our examples, we’ll use the package phase, which includes compiling and running the tests. The options explored throughout this tutorial belong to the Maven Surefire Plugin.

3. Using Command Line Flags

3.1. Skipping the Test Compilation

First, let’s look at an example of a test that doesn’t compile:

@Test
public void thisDoesntCompile() {
    baeldung;
}

When we run the command-line command:

mvn package

We’ll get an error:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/baeldung/skip-tests/src/test/java/com/antmordel/skiptests/PowServiceTest.java:[11,9] not a statement
[INFO] 1 error

Therefore, let’s explore how to skip the compilation phase for the test’s sources. In Maven, we can use the maven.test.skip flag:

mvn -Dmaven.test.skip package

As a result, the test sources are not compiled and, therefore, are not executed.

3.2. Skipping the Test Execution

As a second option, let’s see how we can compile the test folder but skip the run process. This is useful for those cases where we’re not changing the signature of the methods or classes but we’ve changed the business logic, and as a result, we broke the tests. Let’s consider a contrived test case like the one below, which will always fail:

@Test
public void thisTestFails() {
    fail("This is a failed test case");
}

Since we included the statement fail(), if we run the package phase, the build will fail with the error:

[ERROR] Failures:
[ERROR]   PowServiceTest.thisTestFails:16 This is a failed test case
[INFO]
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

Let’s imagine we want to skip running the tests but still we want to compile them. In this case, we can use the -DskipTests flag:

mvn -DskipTests package

and the package phase will succeed.

Finally, it’s worth mentioning that the now-deprecated flag -Dmaven.test.skip.exec will also compile the test classes but will not run them.

4. Using Maven Configuration

In the case that we need to exclude compiling or running the tests for a longer period of time, we can modify the pom.xml file in order to include the proper configuration.

4.1. Skipping the Test Compilation

As we did in the previous section, let’s examine how we can avoid compiling the test folder. In this case, we’ll use the pom.xml file. Let’s add the following property:

<properties>
    <maven.test.skip>true</maven.test.skip>
</properties>

Keep in mind that we can override that value by adding the opposite flag in the command line:

mvn -Dmaven.test.skip=false package

4.2. Skipping the Test Execution

Again, as a second step, let’s explore how we can build the test folder but skip the test execution using the Maven configuration. In order to do that, we have to configure the Maven Surefire Plugin with a property:

<properties>
    <tests.skip>true</tests.skip>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <skipTests>${tests.skip}</skipTests>
    </configuration>
</plugin>

The Maven property tests.skip is a custom property that we previously defined. Therefore, we can override it if we want to execute the tests:

mvn -Dtests.skip=false package

4. Conclusion

In this quick tutorial, we’ve explored all the alternatives that Maven offers in order to skip compiling and/or running the tests. We went through the Maven command line options and the Maven configuration options.


Viewing all articles
Browse latest Browse all 4536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>