我使用以下注释来标记我的集成测试:
@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任务 - 推荐的方法是什么?
答案 0 :(得分:16)
基于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
}
gradlew test
将运行没有集成的测试gradlew integrationTest
将仅运行集成测试gradlew check
将先运行test
,然后运行integrationTest
gradlew integrationTest test
将运行test
,然后运行integrationTest
shouldRunAfter
plugin: 'org.junit.platform.gradle.plugin'
注意:尽管上述方法有效,但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
}
答案 1 :(得分:7)
我提出了一个问题:https://github.com/junit-team/junit5/issues/579(由Sam Brannen建议)。
与此同时,我使用项目属性作为解决方法:
junitPlatform {
filters {
tags {
exclude project.hasProperty('runIntegrationTests') ? '' : 'integration-test'
}
}
}
因此,集成测试将被跳过:
gradle test
但包含:
gradle test -PrunIntegrationTests
答案 2 :(得分:0)
我不确定是不是因为 Gradle 行为发生了变化,但投票最高的答案在 Gradle 中对我不起作用。 6.8.3.我看到了integrationTests 任务与主测试任务一起运行。这个简化版本对我有用:
test {
useJUnitPlatform {
excludeTags "integration"
}
}
tasks.register("integrationTests", Test) {
useJUnitPlatform {
includeTags "integration"
}
mustRunAfter check
}
命令:
./gradlew test
或 ./gradlew clean build
- 没有运行测试
“集成”标签。./gradlew integrationTests
- 只运行测试 with
“集成”标签。答案 3 :(得分:0)
据我所知,解决此问题的最佳当前工作代码是由以下人员提供的代码:TWiStErRob found here。
请注意,在下面的示例中,必须使用 ui 标记测试 (Junit5 tagging):
task uiTest(type: Test) {
useJUnitPlatform {
includeTags 'ui'
excludeTags 'integration'
}
}
然而,我没有设法让 Junit5 test-suit-thing 直接从 gradle 运行,我认为这会是一个更好的解决方案。但我认为购买 TWiStErRob 的解决方案已经足够好了。不利的一面是 gradle.build 文件现在也会因测试服而变得臃肿。
请注意,可以像这样在 gradle 文件中创建多个测试套件:
task firstTestSuite(type: Test) {
useJUnitPlatform {
includeTags 'test-for-first-test-suite'
}
}
task secondTestSuite(type: Test) {
useJUnitPlatform {
includeTags 'test-for-second-test-suite'
}
}
然后所有的都可以像这样单独运行:
gradlew firstTestSuite
gradlew secondTestSuite
gradlew ui
使用 Gradle 6.6.1 运行的解决方案