Skipping tests in some modules in Maven

I would like my Maven builds to run most unit tests. But there are single tests in one project that are slower, and I would like to exclude them altogether; and sometimes include them.

Question : How to do this?

I know about -Dmaven.test.skip=true , but this disables all unit tests.

I also know about skipping the integration tests described here . But I don't have integration tests, just unit tests, and I don't have explicit calls to the maven-surefire-plugin. (I am using Maven 2 with the Eclipse-Maven plugin).

+53
maven unit-testing junit maven-2 build-process
Apr 21 '09 at 14:02
source share
5 answers

How about skipping tests only in this module?

In the pom.xml of this module:

 <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project> 

In the end, you can create a profile that will disable tests (another pom.xml module):

 <project> [...] <profiles> <profile> <id>noTest</id> <activation> <property> <name>noTest</name> <value>true</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </profile> </profiles> [...] </project> 

In the last solution, if you run mvn clean package , it will run all the tests. If you run mvn clean package -DnoTest=true , it will not run tests for this module.

+61
Apr 21 '09 at 14:10
source share

I think it is simpler and also useful to work for non-surefire tests (in my case, FlexUnitTests)

 <profile> <id>noTest</id> <properties> <maven.test.skip>true</maven.test.skip> </properties> </profile> 
+25
Mar 11
source share

I had a slightly different need for this question, which may be useful. I wanted to exclude several different tests from different packages from the command line, so a single template would not do that.

In the Maven Failsafe documentation rules for exceptions, I found that you can specify a comma-delimited list for both regular expressions and patterns: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion -exclusion.html

So my pomfile looked like this:

 <excludes> <exclude>${exclude.slow.tests}</exclude> </excludes> 

and my command line included the following:

 mvn install "-Dexclude.slow.tests=**/SlowTest1.java, **/package/ofslowtests/*.java, **/OtherSlowTest.java" 

For me, the key component was getting a bunch of tests into one maven property in one exclude declaration.

+2
Dec 08 '16 at 22:48
source share

If you have a large multi-module project and you want to skip tests only in certain modules without having to modify each of the pom.xml module files using custom configuration and profiling, you can add the following to the parent pom.xml file:

 <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.12</version> <executions> <execution> <id>regex-property</id> <goals> <goal>regex-property</goal> </goals> <configuration> <name>maven.test.skip</name> <value>${project.artifactId}</value> <regex>(module1)|(module3)</regex> <replacement>true</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin> </plugins> </build> <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> 

Thanks to build-helper-maven-plugin you actually dynamically check whether you are in a particular module or not during the build using the project.artifactId property (pointing to each artifactId module during build), then regex find matches for specific values (the names of the modules for which you want to skip tests) and appropriately fill the maven.test.skip property (setting it to true ).

In this case, the tests will be skipped for module1 and module3 when working properly for module2 , that is, as expressed by the regular expression.

The advantage of this approach is to make it dynamic and centralized (in the parent pom.xml ), therefore, better for maintenance: you could add or remove modules at any time simply by changing the simple regular expression above.

Obviously, if this is not the default behavior for the assembly (recommended case), you can always wrap the snippet above in the maven profile .




You can also go further and have dynamic behavior based on your input:

 <properties> <test.regex>none</test.regex> </properties> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.12</version> <executions> <execution> <id>regex-property</id> <goals> <goal>regex-property</goal> </goals> <configuration> <name>maven.test.skip</name> <value>${project.artifactId}</value> <regex>${test.regex}</regex> <replacement>true</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin> </plugins> </build> 

Here we actually replace the regex test.regex with the test.regex property with the default value of none (or any that would not correspond to the name of any module or, in addition, to the default skipping values).

Then from the command line we could have

 mvn clean test -Dtest.regex="(module1)" > will skip tests only for module1 mvn clean test -Dtest.regex="(module1)|(module2)" > will skip tests on module1 and module2 mvn clean test -Dtest.regex="(module1)|(module2)|(module3)" > will skip the three module tests mvn clean test -Dtest.regex=".+" > will skip all module tests mvn clean test > would not skip anything (or fall back on default behavior) 

That is, at runtime you decide without having to modify the pom.xml file or activate any profile.

+1
Sep 18 '16 at 20:29
source share

Using Surefire Plugin 2.19, you can simply exclude those tests that you do not want to use with regular expressions:

mvn '-Dtest=!%regex[.*excludedString.*]' test

The above command will exclude all tests containing exceptString.

NB1 If a double quotation mark (") is used instead of the apostrophe ('), the command will not be interpreted properly and will lead to unexpected results. (Tested using bash 3.2.57)

NB2 Particular attention should be paid to projects that use several versions of the surefire plugin. Versions of surefire older than 2.19 will not run any tests because they do not support regular expressions.

Version control (it might be a good idea to add this to the parent pom file):

 <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> </plugin> </plugins> </pluginManagement> </build> 

Examples of build commands that pass tests: https://artbcode.com/how-to-skip-a-subset-of-the-unit-tests/

+1
Nov 04 '16 at 19:07
source share



All Articles