Very often you might need to build your code despite of the test cases not working fine. But what we mean by skipping the test cases? Well, this means that while creating a maven build, we want the test cases to be ignored from the build. We want them not to execute; in fact we may also want them not be compiled even.
Why to Skip Unit Tests:
There can be several scenarios in which you would not want the test cases to be considered in build. Few of them may be:
- Someone else’s module has some unit tests which fail and you don’t need to care
- There is some legacy code into the system for which few non-working test cases exist and now you need to ignore them
- If your project takes long time to run through all unit tests then you may want to speed up the overall build. Which you can do by avoiding running the unit tests in the different modules. And if your project is having multiple modules, you may not want to go in and edit each individual pom.xml to achieve this.
Two ways to Ignore the Test Cases
There are two different ways to do it which can be chosen based on requirement:
(A) Ignore Test cases from Command Line itself:
There are two different commands using which test cases would be ignored:
a) This option skips compiling the tests and the execution of them. This is a feature of maven itself.
$mvn install -Dmaven.test.skip=true
b) This option compiles the tests, but skips executing or running them and this is a feature of surefire which we are going to see more in detail next.
$mvn install -DskipTests=true
Disadvantage: There is a drawback with skipping the tests from the command line. In a multi-module build environment, this would disable all tests across all the modules. You may not want to do that always. Also, every time you are making the build, you have to remember to use the command line option to ignore the test cases.
Let’s see what can be the other way to avoid the test cases in maven build.
(B) Using Maven Surefire Plugin
To get rid of the disadvantages discussed above and get more control of running a subset of tests for any module, we can use the Maven Surefire Plugin’s test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. There is a plugin available from Maven which can be configured in the maven POM file. For example have a look at the following POM segment:
Inclusions and Exclusions of Tests
By default, the Surefire Plugin automatically includes all test classes with the following wildcard patterns:
• “/Test*.java” – includes all of its subdirectories and all java filenames that start with “Test”.
• “/*Test.java” – includes all of its subdirectories and all java filenames that end with “Test”.
• “**/*TestCase.java” – includes all of its subdirectories and all java filenames that end with “TestCase”.
If the test classes do not go with the above mentioned naming conventions, then we need to configure Surefire Plugin and specify the tests we are looking to include.
Like we discussed in the beginning of the article itself there may be cases where some tests are causing the build to fail. Excluding them is one of the best workarounds to continue the build. Exclusions can be done by configuring the excludes property of the plugin.
Now it’s your turn:
What do you do in your project if any of the test cases fail and halts the build? Do you avoid them or not in the build then? If yes, how and if not, what is the approach do you take? Share your experience with us in the comments section.