You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JUnit测试套件@IncludeTags仍运行无@Tag的JUnit4测试,如何仅执行带标签测试?

解决方案

要实现只运行带@Tag("myTag")的JUnit 5测试、忽略标注@RunWith的JUnit 4测试,只需在测试套件中添加@ExcludeAnnotations注解排除带有@RunWith的类,结合原有的@IncludeTags即可:

@Suite
@SelectPackages("my.package")
@IncludeTags("myTag")
@ExcludeAnnotations(RunWith.class)
public class myTestSuite {
}

注解说明

  • @ExcludeAnnotations(RunWith.class):直接排除所有标注了@RunWith的测试类,也就是你项目里的JUnit 4测试。
  • @IncludeTags("myTag"):保留只带有指定Tag的JUnit 5测试——因为JUnit 4测试不支持@Tag注解,本来就不会被这个规则选中,加上@ExcludeAnnotations是更彻底的排除方式。

另外要确保你的项目已经引入了JUnit 5 Suite相关依赖,比如junit-platform-suite-api和junit-platform-suite-engine,否则套件无法正常工作。

内容的提问来源于stack exchange,提问作者BlairCalgary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 05:20:56