我使用以下注释标记集成测试:
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("integration-test")
public @interface IntegrationTest {
}
这是我在build.gradle
中使用的过滤器,用于从gradle build
中排除这些测试:
junitPlatform {
filters {
tags {
exclude 'integration-test'
}
}
}
到目前为止,很好。
现在,我想提供一个专门运行我的集成测试的Gradle任务-推荐的方法是什么?
基于https://github.com/gradle/gradle/issues/6172#issuecomment-409883128
于2020年修订,以考虑惰性任务配置和Gradle 5。有关较旧版本,请参见答案的历史记录。
plugins {
id "java"
}
def test = tasks.named("test") {
useJUnitPlatform {
excludeTags "integration"
}
}
def integrationTest = tasks.register("integrationTest2", Test) {
useJUnitPlatform {
includeTags "integration"
}
shouldRunAfter test
}
tasks.named("check") {
dependsOn integrationTest
}
gradlew test
将运行没有集成的测试gradlew integrationTest
将仅运行集成测试gradlew check
将先运行test
,然后再运行integrationTest
gradlew integrationTest test
将先运行test
,然后再运行integrationTest
注意:由于shouldRunAfter
plugin: 'org.junit.platform.gradle.plugin'
注意:虽然上述方法有效,但IntelliJ IDEA很难推断出内容,因此,我建议使用更明确的版本,其中键入所有内容并完全支持代码完成]]:
... { Test task -> task.useJUnitPlatform { org.gradle.api.tasks.testing.junitplatform.JUnitPlatformOptions options -> options.includeTags 'integration' } }
build.gradle.kts
用于在Gradle 5.6.4中的所有模块中配置集成测试的根项目Kotlin DSL插件
allprojects {
plugins.withId("java") {
@Suppress("UnstableApiUsage")
[email protected] {
val test = "test"(Test::class) {
useJUnitPlatform {
excludeTags("integration")
}
}
val integrationTest = register<Test>("integrationTest") {
useJUnitPlatform {
includeTags("integration")
}
shouldRunAfter(test)
}
"check" {
dependsOn(integrationTest)
}
}
}
}
我提出了一个问题:https://github.com/junit-team/junit5/issues/579(由Sam Brannen建议)。