来自 Maven,我正在探索 Gradle 作为替代方案。技术:Java 11、jUnit 5、Maven 3.6、Gradle 5.6。
我陷入了配置集成测试的困境。遵循 Maven 的 Surefire 和 Failsafe 插件的默认命名约定,我的测试位于标准测试目录中,并通过后缀进行区分:单元测试以 Test.java
结尾,集成测试以 IT 结尾.java
.
是否可以在 Gradle 中进行相同的设置?到目前为止我见过两种方法:
- 使用 jUnit5 的标签(这意味着我必须去标记每个集成测试)
- 使用单独的目录进行单元测试和集成测试
理想情况下,我希望保持文件夹结构不变,因为它会影响多个 git 存储库。
最佳答案
好的,我想我成功地让它与源集一起工作,感谢 Lukas 的反馈和 Slaw .
如果可以改进,请告诉我:
// define the dependencies of integrationTest, inherit from the unit test dependencies
configurations {
integrationTestImplementation.extendsFrom(testImplementation)
integrationTestRuntimeOnly.extendsFrom(testRuntimeOnly)
}
sourceSets {
test {
java {
// exclude integration tests from the default test source set
exclude "**/*IT.java"
}
}
// new source set for integration tests
integrationTest {
// uses the main application code
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
java {
// the tests are at the same directory (or directories) as the unit tests
srcDirs = sourceSets.test.java.srcDirs
// exclude the unit tests from the integration tests source set
exclude "**/*Test.java"
}
resources {
// same resources directory (or directories) with the unit tests
srcDirs = sourceSets.test.resources.srcDirs
}
}
}
// define the task for integration tests
task integrationTest(type: Test) {
description = "Runs integration tests."
group = "verification"
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
shouldRunAfter test
// I'm using jUnit 5
useJUnitPlatform()
}
// make the check task depend on the integration tests
check.dependsOn integrationTest
关于java - 使用 Maven 的命名约定在 Gradle 中进行集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58645456/