2

I'm trying to let maven-surefire-plugin only run all the tests fulfilling following conditions:

  • Match the pattern *Test.java, but NOT *SomeTest.java;
  • OR match the pattern *AnotherSomeTest.java.

Is there any way to achieve this?

I've already tried the include string:

**/*Test.java, !%regex[.*(?!Another)SomeTest.*]

however this does not work.

xiGUAwanOU
  • 325
  • 2
  • 16

1 Answers1

4

There are several solutions possible using a regular expression. The simplest one is having the configuration

<includes>
  <include>*AnotherSomeTest.java</include>
  <include>%regex[.*(?&lt;!Some)Test\.class]</include>
</includes>

This will match tests where the class name ends either with AnotherSomeTest, or with Test but not preceded by Some (the negative lookbehind < just needs to be properly escaped with &lt;). Note that the regular expression match is done over the .class file, while the traditional one is done over the .java file.

You could put this into a single regular expression, with

<include>%regex[.*(?&lt;!(?&lt;!Another)Some)Test\.class]</include>

which selects all tests ending with Test, not preceded by Some themselves not preceded by Another, although it's probably not clearer.

Another alternative is to use multiple formats into one, that works since version 2.19 of the plugin and JUnit 4 or TestNG. This is useful for the command line notably:

<include>%regex[.*(?&lt;!Some)Test\.class], *AnotherSomeTest.java</include>
Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423