0

I have a scenario where i need to exclude certain tests in a module from running during the compilation. For that, i have a created a profile in that module's pom.xml as shown below,

<profiles>
        <profile>
            <id>excludeTests</id>
            <activation>
                <property>
                    <name>excludeTests</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <excludes>
                                <exclude>com/abc/def/MyPackage</exclude>
                            </excludes>

                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

In the command line, I am running the below command,

mvn clean test -DexcludeTests=true

The junit classes present under 'MyPackage' is still getting executed. Is my profile really getting picked. Please help me here to exclude the junits from this package while building.

vinod hy
  • 827
  • 2
  • 14
  • 26
  • Why skipping the tests? Do they not work? If so you should remove them because they have no value ...Furthermore depending on JUnit you can annotate them with `@Ignore` ... – khmarbaise Apr 30 '21 at 07:45
  • They work. No issues there. As i mentioned, i just need to check health of my code base. The test cases which i want to ignore is not managed by our team. I just need to check my code and its test cases only. – vinod hy Apr 30 '21 at 08:21
  • 1
    So why skipping the tests if you like check the health of your code? Sounds like a contradiction? – khmarbaise Apr 30 '21 at 12:52

1 Answers1

0

The usual approach to do this in Maven is via profiles but if you don't want to touch the pom.xml, you can create a @Rule (in Junit 4) to skip tests based on some property you set in command line or as an environment variable (useful for CI).

Here's an example:

public class SkippableTestRule implements TestRule {

   private final boolean SKIP_TESTS = System.getProperty( "skipSelectedTests", false );

   @Override
   public Statement apply(Statement base, Description description) {
        if ( SKIP_TESTS && skippablePackage()) {
            return new SkipTest();
        }
        return base;
    }

    private static class SkipTest extends Statement {

        private SkipTest() {
        }

        @Override
        public void evaluate() throws Throwable {
            Assume.assumeTrue( "Skipping test", false );
        }
    }
}

Now that you have defined this rule, you can apply it to a test class this way:

class MyTests {

    @Rule
    public SkippableTestRule rule = new SkSkippableTestRule();
}

Now all the tests in the class will be ignored when you run the build with:

mvn -DargLine="-DskipSelectedTests=true"

If this is not good enough, you can create a Test Unit runner instead. This will give you more control on which test class is running.

For JUnit 5, you can do something similar with extensions.

Alternatively, you can create a profile where you configure the tests in a different way:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.12.4</version>
   <configuration>
     <excludes>
       <exclude>**/acme/package/</exclude>
     </excludes>
   </configuration>
 </plugin>

You can also define an excludes pattern. See the documentation for more details.

The profiles will look something like this:

<profiles>
 <profile>
  <id>default-profile</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <plugins>
  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12.4</version>
    </plugin>
 </plugins>
</profile> 
</profile>
  <profile>
    <id>exclude-tests</id>
    <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12.4</version>
      <configuration>
        <excludes>
          <exclude>**/acme/package</exclude>
        </excludes>
      </configuration>
    </plugin>
    </plugins>
  </profile>
</profiles>
...

You can then run the specific profile with:

mvn test -P exclude-tests
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30