Is there a way to skip only a single test in maven?

You can specify an exclusion pattern to -Dtest option by prefixing it with an ! (exclamation mark). E.g.,

mvn -Dtest=\!FlakyTest* install

Found it in here and verified to be working. E.g., I was able to skip this flaky Jenkins test by using:

mvn -Dtest=\!CronTabTest* package

It is normal that integration tests need to be excluded but unit tests need to be included. To achieve this I suggest naming all integration tests with a postfix IntegrationTest (e.g. AbcIntegrationTest.java).

and then in your maven build put the following:

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>

When you build with this all the integration tests wil be excluded, but all other tests (Unit test for example) are run. Perfect :-)

For more info on exclusing and including tests during the test run, read

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

PS To exlude one single test you simply need to explicitly name it in the exclude list. Easy.


With junit 4, I add an @Ignore annotation when I want to do that. This would work for you, unless you want to only sometimes ignore the test, or only ignore it when the build runs from maven. If this is the case, then I would ask "Why?"

Tests should be consistent, they should be portable, and they should always pass. If a specific test is problematic I would consider re-writing it, removing it entirely, or moving it to a different test suite or project.


If you want to use the CLI to exclude one single test you should use the -Dtest and -Dit.test flags.

Be careful to reset the defaults. When you are using the ! notation all the defaults are erased and you should put them back. For normal tests executed by surefire you should add *Test, Test*, *Tests, *TestCase, while for integration tests executed by failsafe you should add IT*, *IT, *ITCase.

You can find more info here https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html (normal tests)

and here https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html (integration tests)

-Dit.test='!ITsometestIT, IT*, *IT, *ITCase'

A complete mvn command could be this one:

mvn -e -B -Dtest='!unitTestABC, *Test, Test*, *Tests, *TestCase' -Dit.test='!ITintegrationTestABCIT, IT*, *IT, *ITCase' -DfailIfNoTests=false clean install

Remember to use ' and NOT ". When using double quotes any ! inside them will be evaluated by bash.

Remember also that integration tests are not run when mvn test. With mvn verify only integration tests will be run and not unit tests