从命令行使用 Maven Surefire + JUnit5 包含/排除测试

Include / exclude tests with Maven Surefire + JUnit5 from command line

我想 运行 Apache James 的构建,它有一个巨大的测试套件,由于与我无关的测试,例如 运行ning 很长关于 RabbitMQ 的测试。因此我想排除那些并且我想从命令行这样做(而不是通过编辑 POM)。我在 Java 11 OpenJDK 上使用 Maven 3.6.3。项目使用JUnit5和maven-surefire-plugin 2.22.2.

现在,我希望 the following 可以工作:

For example, to run only test methods in the org.example.MyTest test class you can execute mvn -Dtest=org.example.MyTest test from the command line.

但是没用。事实上,一旦我将 test 参数设置为 anything 除了空字符串,all 测试将被跳过。我尝试了一些 syntax that is supposedly supported...

mvn package -Dtest=*
mvn package -Dtest=".*"
mvn package -Dtest=\!SomethingFishy
mvn package -Dtest='!MavenHeadache'
mvn package -Dtest='!%regex[.*HelpMe.*]'

...但结果总是一样的:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
  (default-test) on project testing-base: No tests were executed!  
  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]

我正在 运行 实现 package 目标,因为它完成了完整构建,但 test 行为相同。还尝试指定 -Dincludes=... / -Dexcludes=...,这根本没有效果:所有测试都会执行,并且参数甚至不会显示在 mvn -X ... 的输出中。当将 maven-surefire-plugin 更新到最新版本 3.0.0-M5 时,此行为不会改变。

我是不是理解错了?我如何在此设置中指定 inclusions/exclusions?


更新: 看起来这是由嵌套项目引起的 and/or 特别是 James 的项目结构。如果我输入一个“叶子项目”,例如core,然后inclusions/exclusions开始工作:

cd core
mvn test -Dtest=HostTest         # will only run HostTest, as expected
mvn test -Dtest=\!HostTest       # will run all tests but HostTest, as expected

按照 RobertScholte 的建议,我查看了 maven-surefire-plugin 配置,但找不到任何似乎与此行为相关的内容。

错误消息告诉你需要知道的:在第一个到运行的项目testing-base中,没有匹配你的模式的测试,所以它不能确保你不会有成功的假象。

然后它建议使用 -DfailIfNoTests=false 选项让 maven 忽略没有任何测试匹配模式的模块(这可能是你需要的)。