maven: How can I skip test in some projects via command line options?
Asked Answered
W

4

77

In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?

My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.

I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?

Wileywilfong answered 3/2, 2012 at 2:51 Comment(3)
possible duplicate of Skipping tests in some modules in MavenPoaceous
It's very annoying that the maven developers expect you do use pom.xml instead of command line options. And 95% of Stack Overflow answers keep giving pom.xml solutions.Selfaggrandizement
mvn clean install -DskipTestsPastorship
H
99

To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.

If you need more fine grain control of running a subset of tests for a module, look into using 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. Take for example the following POM segment:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <excludes>
            <exclude>${someModule.test.excludes}</exclude>
          </excludes>
          <includes>
            <include>${someModule.test.includes}</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <someModule.skip.tests>false</someModule.skip.tests>
    <skipTests>${someModule.skip.tests}</skipTests>
    <someModule.test.includes>**/*Test.java</someModule.test.includes>
    <someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
  </properties>

With a POM like the above you can execute tests in a variety of ways.

  1. Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
  1. Skip all tests across all modules
mvn -DskipTests=true test
  1. Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
  1. Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
  1. Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Hessenassau answered 4/2, 2012 at 4:4 Comment(5)
I think you're missing a double-quote after ExcludeTest.javaSelfaggrandizement
@Sridhar-Sarnobat thanks. the answer has been updated.Hessenassau
Is <skipTests>${module.skip.tests}</skipTests> supposed to be <skipTests>${someModule.skip.tests}</skipTests>?Verify
which one is skip compiling tests and which is skip running tests (though they are compiled)Admirable
I am not aware of a flag that skips test compilation. The answer only deals with test execution.Hessenassau
K
18

Found a way to exclude on command line:

# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method 
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*

This is from: Run one or Exclude one test with Maven

Khedive answered 18/6, 2018 at 20:35 Comment(5)
@Arjun, which ones? I used some of these at the time, not sure if I tried everything.Khedive
# Exclude one test class, by using the explanation mark (!) mvn test -Dtest=!LegacyTestHalutz
@Arjun, IIRC I used 'Exclude one test method`. But please tell what worked and what didn't for you. Would be useful for other users.Khedive
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ ot_osefalf_automation_test --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] - [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project ot_osefalf_automation_test: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]Halutz
from command line with \! mvn test -Dtest=\!LegacyTestColtoncoltsfoot
O
13

…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use -Darguments=-DskipTests to get it work.

Orit answered 12/12, 2012 at 15:28 Comment(0)
P
5

If you want to use Maven profiles:

you might want to make it work doing something like this:

I don't know if there is a supported command line option that does the same.

You also might try using environment properties directly, something as per this doc page:

i.e. something like:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
      <skipTests>${moduleA.skipTests}</skipTests>
    </configuration>
  </plugin>

then using mvn -DmoduleA.skipTests=false test to test that one module.

Pownall answered 3/2, 2012 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.