Gradle 在其他源集中找不到测试
Gradle can't find tests in additional source set
我的目标是为集成测试设置源集。我创建了一个名为 "intTest" 的源集。我在里面放了一个简单的测试:
package com.github.frozensync;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Application {
@Test
@DisplayName("should work")
void foo() {
assertEquals(2, 2);
}
}
当我尝试 运行 它时,出现以下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.github.frozensync.Application](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0ms
这是我的项目结构:
这是我的 build.gradle
plugins {
id 'java'
}
group 'com.github.frozensync'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
intTest {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
idea {
module {
testSourceDirs += sourceSets.intTest.java.srcDirs
testResourceDirs += sourceSets.intTest.resources.srcDirs
}
}
configurations {
intTestImplementation.extendsFrom implementation
intTestRuntimeOnly.extendsFrom runtimeOnly
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
intTestImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}
test {
useJUnitPlatform()
}
task integrationTest(type: Test) {
description = 'Runs integration tests.'
group = 'verification'
testClassesDirs = sourceSets.intTest.output.classesDirs
classpath = sourceSets.intTest.runtimeClasspath
shouldRunAfter test
}
check.dependsOn integrationTest
我关注了 guide by Gradle。当我遇到这个问题时,我尝试了以下方法:
- 运行 ./gradlew cleanIntegrationTest integrationTest
绕过 IntelliJ 但它仍然 运行 0 次测试
- 添加 the idea Gradle plugin.
- 从 this.
添加 dependsOn
- 来自 this 的解决方案。
如何启用 Gradle 来发现源集 "intTest" 中的测试?
您的测试是 JUnit 5 测试,但您没有告诉 Gradle。就像你需要调用的测试任务一样
useJUnitPlatform()
在您的 integrationTest
任务的配置中。
我的目标是为集成测试设置源集。我创建了一个名为 "intTest" 的源集。我在里面放了一个简单的测试:
package com.github.frozensync;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Application {
@Test
@DisplayName("should work")
void foo() {
assertEquals(2, 2);
}
}
当我尝试 运行 它时,出现以下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.github.frozensync.Application](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0ms
这是我的项目结构:
这是我的 build.gradle
plugins {
id 'java'
}
group 'com.github.frozensync'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
intTest {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
idea {
module {
testSourceDirs += sourceSets.intTest.java.srcDirs
testResourceDirs += sourceSets.intTest.resources.srcDirs
}
}
configurations {
intTestImplementation.extendsFrom implementation
intTestRuntimeOnly.extendsFrom runtimeOnly
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
intTestImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}
test {
useJUnitPlatform()
}
task integrationTest(type: Test) {
description = 'Runs integration tests.'
group = 'verification'
testClassesDirs = sourceSets.intTest.output.classesDirs
classpath = sourceSets.intTest.runtimeClasspath
shouldRunAfter test
}
check.dependsOn integrationTest
我关注了 guide by Gradle。当我遇到这个问题时,我尝试了以下方法:
- 运行 ./gradlew cleanIntegrationTest integrationTest
绕过 IntelliJ 但它仍然 运行 0 次测试
- 添加 the idea Gradle plugin.
- 从 this.
添加 dependsOn
- 来自 this 的解决方案。
如何启用 Gradle 来发现源集 "intTest" 中的测试?
您的测试是 JUnit 5 测试,但您没有告诉 Gradle。就像你需要调用的测试任务一样
useJUnitPlatform()
在您的 integrationTest
任务的配置中。