如何使Maven Surefire插件包含正则表达式与特定前缀不匹配?

时间:2017-03-02 15:20:56

标签: maven maven-surefire-plugin

我试图让maven-surefire-plugin只运行满足以下条件的所有测试:

  • 匹配模式*Test.java,但不是*SomeTest.java;
  • 或匹配模式*AnotherSomeTest.java

有没有办法实现这个目标?

我已经尝试过包含字符串:

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

然而这不起作用。

1 个答案:

答案 0 :(得分:3)

使用a regular expression可能有多种解决方案。最简单的就是配置

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

这将匹配类名称与AnotherSomeTestTest but not preceded by Some结束的测试(负面的lookbehind <只需要&lt;正确转义1}})。请注意,正则表达式匹配是通过.class文件完成的,而传统的表达式是通过.java文件完成的。

您可以使用

将其放入单个正则表达式中
<include>%regex[.*(?&lt;!(?&lt;!Another)Some)Test\.class]</include>

选择所有以Test结尾的测试,而不是Some之前没有Another的测试,但它可能不会更清晰。

另一个替代方法是使用multiple formats into one,它自插件版本2.19和JUnit 4或TestNG起作用。这对命令行非常有用:

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