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

Viewed 58240

I would like to skip only a single test while launching mvn install.

Is there a way to do that ?

6 Answers

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

i think this should work if using this command:

mvn archetype:create -DgroupId=test -DartifactId=test

(for test change pom.xml and test-class to the following and use mvn install)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<url>http://maven.apache.org</url>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        test/AppTest.java
              </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>

test-class:

package test;
import org.junit.Test;
import static org.junit.Assert.fail;
public class AppTest 
{
    @Test
    public void test_it() {
        fail("not implemented");
    }
}
Related