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

如何在build.gradle.kts中为不同JUnit5标签创建多测试覆盖任务

Gradle Kotlin DSL 实现按Tag执行JUnit 5测试的自定义任务

在build.gradle.kts中,你可以通过注册自定义Test类型任务,配合JUnit 5的标签过滤功能,实现只运行指定标签测试的需求。以下是对应run_type1_tests和run_type2_tests任务的等价写法:

tasks.register<Test>("run_type1_tests") {
    description = "只运行标记为@Tag(\"Type1\")的JUnit 5测试"
    useJUnitPlatform {
        includeTags("Type1")
    }
}

tasks.register<Test>("run_type2_tests") {
    description = "只运行标记为@Tag(\"Type2\")的JUnit 5测试"
    useJUnitPlatform {
        includeTags("Type2")
    }
}

说明:

  • 每个任务都继承自Gradle的Test类型,保留标准测试任务的所有特性(如测试报告生成、类路径配置等)
  • useJUnitPlatform()启用JUnit 5测试引擎支持
  • includeTags()方法指定要执行的测试标签,与测试类/方法上@Tag注解的取值完全对应

执行任务时,直接通过Gradle命令调用即可:

./gradlew run_type1_tests
# 或者执行另一个任务
./gradlew run_type2_tests

内容的提问来源于stack exchange,提问作者Ryu S.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 07:32:33