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

I would like to skip only one test when running mvn install .

Is there any way to do this?

+56
maven-2 surefire
May 7 '09 at 16:26
source share
6 answers

In junit 4, I add @Ignore annotation when I want to do this. This will work for you if you don't want to just ignore the test sometimes or ignore it only when the build is running from maven. If so, I would ask: "Why?"

Tests must be consistent, they must be portable, and they must always pass. If a particular test is problematic, I would think about re-writing it, completely deleting it or moving it to another test suite or project.

+21
May 07, '09 at 16:49
source share

You can specify an exception pattern for the -Dtest parameter by specifying it ! (Exclamation point). For example.

 mvn -Dtest=\!FlakyTest* install 

Found it in here and confirmed that it works. For example, I was able to skip this uneven Jenkins test using:

 mvn -Dtest=\!CronTabTest* package 
+42
Jun 13 '16 at 15:55
source share

It is normal that integration tests should be excluded, but unit tests should be included. For this, I propose to name all integration tests with postfix IntegrationTest (for example, AbcIntegrationTest.java).

and then in your Maven assembly 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 come up with this, all integration tests will be excluded, but all other tests (for example, unit test) are executed. Fine :-)

For more information on the exclusion and inclusion of tests during test execution, read

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

PS To exclude one single test, you just need to explicitly specify it in the list of exceptions. Easily.

+7
Sep 26 '13 at 9:25
source share

Check out 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 with a set of tests:

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

You can also include ( groups ) / exclude ( excludedGroups ) those tests for which maven uses as an example:

 mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test 
+6
Dec 09 2018-11-11T00:
source share

I think this should work if you use this command:

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

(for the test change pom.xml and test-class for the next and using 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"); } } 
+4
May 19 '09 at 7:23 a.m.
source share

If you want to use the CLI to exclude one test, you must use the -Dtest and -Dit.test .

Be careful to reset defaults. When you use notation ! , all default values ​​are erased and you must return them back. For regular tests performed by surefire , you must add *Test, Test*, *Tests, *TestCase , while for integration tests performed by failsafe , you must add IT*, *IT, *ITCase .

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

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

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

The full mvn could be:

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

Remember to use ' , NOT. " When using double quotes, any ! Within them, bash will be evaluated.

Remember also that integration tests do not run with mvn test . With mvn verify only integration tests will be performed, not unit tests

+1
Oct 26 '18 at 12:18
source share



All Articles