JUnit5特定于标记的gradle任务

Rah*_*thy 17 gradle junit5

我使用以下注释来标记我的集成测试:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("integration-test")
public @interface IntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

这是我用来build.gradlegradle build以下方面排除这些测试的过滤器:

junitPlatform {
    filters {
        tags {
            exclude 'integration-test'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

现在我想提供一个专门运行我的集成测试的Gradle任务 - 推荐的方法是什么?

TWi*_*Rob 26

基于https://github.com/gradle/gradle/issues/6172#issuecomment-409883128

test {
    useJUnitPlatform {
        excludeTags 'integration'
    }
}

task integrationTest(type: Test) {
    useJUnitPlatform {
        includeTags 'integration'
    }
    check.dependsOn it
    shouldRunAfter test
}
Run Code Online (Sandbox Code Playgroud)

运行

  • gradlew test 将运行测试而不进行集成
  • gradlew integrationTest 将只运行集成测试
  • gradlew check将运行test随后integrationTest
  • gradlew integrationTest test将运行test后跟integrationTest
    注:由于交换订单shouldRunAfter

历史

小费

注意:虽然上面的工作,IntelliJ IDEA很难推断出东西,所以我建议使用这个更明确的版本,其中输入所有内容并完全支持代码完成:

tasks.withType(Test) { Test task ->
    task.useJUnitPlatform { org.gradle.api.tasks.testing.junitplatform.JUnitPlatformOptions options ->
        options.excludeTags 'integration'
    }
}

task integrationTest(type: Test) { Test task ->
    task.useJUnitPlatform { org.gradle.api.tasks.testing.junitplatform.JUnitPlatformOptions options ->
        options.includeTags 'integration'
    }
    tasks.check.dependsOn task
    task.shouldRunAfter tasks.test
}
Run Code Online (Sandbox Code Playgroud)


ete*_*ech 13

等级 6

我不确定是否是因为 Gradle 行为发生了变化,但投票最高的答案在 Gradle 中对我不起作用。6.8.3. 我看到 IntegrationTests 任务与主要测试任务一起运行。这个简化版本对我有用:

test {
    useJUnitPlatform {
        excludeTags "integration"
    }
}

tasks.register("integrationTests", Test) {
    useJUnitPlatform {
        includeTags "integration"
    }
    mustRunAfter check
}
Run Code Online (Sandbox Code Playgroud)

命令:

  • ./gradlew test或- 运行没有./gradlew clean build“集成”标签的测试 。
  • ./gradlew integrationTests- 仅运行带有“集成”标签的测试 。


Rah*_*thy 7

我提交了一个问题:https://github.com/junit-team/junit5/issues/579(由Sam Brannen建议).

同时,我使用项目属性作为解决方法:

junitPlatform {
    filters {
        tags {
            exclude project.hasProperty('runIntegrationTests') ? '' : 'integration-test'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,将跳过集成测试:

gradle test

但将包含在:

gradle test -PrunIntegrationTests