Question

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

Is there a way to do that ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Have a look at this solution, using @Category annotations

public class AccountTest {

    @Test
    @Category(IntegrationTests.class)
    public void thisTestWillTakeSomeTime() {
        ...
    }

    @Test
    @Category(IntegrationTests.class)
    public void thisTestWillTakeEvenLonger() {
        ...
    }

    @Test
    public void thisOneIsRealFast() {
        ...
    }
}

Which you then run using a test suite:

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { AccountTest.class, ClientTest.class })
public class LongRunningTestSuite {}

You also can include (groups) / exclude (excludedGroups) those tests with maven using for exemple:

mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test

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");
    }
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top