Fork me on GitHub

Inclusions and Exclusions of Tests

Inclusions

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

  • "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
  • "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
  • "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
  • "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

If the test classes do not follow any of these naming conventions, then configure Surefire Plugin and specify the tests you want to include.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <includes>
            <include>Sample.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Exclusions

There are certain times when some tests are causing the build to fail. Excluding them is one of the best workarounds to continue the build. Exclusions can be done by configuring the excludes property of the plugin.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <excludes>
            <exclude>**/TestCircle.java</exclude>
            <exclude>**/TestSquare.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Regular Expression Support

An include/exclude pattern can be an ant-style path expression, but regular expressions are also supported through this syntax:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <includes>
            <include>%regex[.*(Cat|Dog).*Test.*]</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Note the syntax %regex[expr], where expr is the actual expression and the rest is just wrapping. Also note the following about the use of regular expressions:

  • Regex matches are done over *.class files and not *.java files
  • Regex matches are done over paths using slashes ("/") and not package names using dots ("."), so the "." in pkg.*Slow.*.class is a regex metacharacter, which happens to match any character, notably the (forward) slashes ("/") that make up the path. Slashes here are forward, even on Windows
  • The trailing .class is interpreted literally, and not as a regular expression ("\.class" does not work here)

Multiple Formats in One

As of Surefire Plugin 2.19, a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):

  [...]
          <include>%regex[.*(Cat|Dog).*], !%regex[pkg.*Slow.*.class], pkg/**/*Fast*.java, Basic????, !Unstable*</include>
  [...]
          <exclude>%regex[pkg.*Slow.*.class], Unstable*</exclude>
  [...]

This syntax can be used in parameters: test, includes, excludes, includesFile, excludesFile. Exclamation mark (!) excludes tests. The syntax in parameter excludes and excludesFile should not use (!). The character (?) within non-regex pattern replaces one character in file name or path. The file extensions are not mandatory in non-regex patterns, and packages with slash can be used. The regex validates fully qualified class file. The regex supports '.class' file extension only. Note the regex comments, marked by (#) character, are unsupported.

Fully qualified class name

As of Surefire Plugin 2.19.1, the syntax with fully qualified class names or packages can be used, e.g.:

  [...]
          <include>my.package.*, another.package.*</include>
  [...]
          <exclude>my.package.???ExcludedTest, another.package.*ExcludedTest</exclude>
  [...]

The character (?) replaces single character and (*) represents zero or more characters. Multiple formats can be additionally combined. This syntax can be used in parameters: test, includes, excludes, includesFile, excludesFile.