提问人:Tyler DeWitt 提问时间:11/16/2023 更新时间:11/17/2023 访问量:15
在不指定项目的情况下,根据多项目 gradle 项目中的类名运行测试
Run a test based off the class name in a multi-project gradle project without specifying the project
问:
以下是该项目的简化版本:
/root
-- /project1
--- /CoolPackage
---- /tests
---- MyTest.kt
-- /project2
--- /NotCoolPackage
---- /tests
---- TheOtherTest.kt
我们将一些测试标记为“集成测试”,如下所示@Tag("integration-test")
假装并拥有该标签。MyTest
TheOtherTest
这是自定义 gradle 任务
fun TaskContainerScope.integrationTestsNamedTask(taskName: String) {
val testName = project.property("testName") as String
println(testName)
register<Test>(taskName) {
description = "Run specific integration test"
shouldRunAfter("test")
minHeapSize = "512m"
maxHeapSize = "1024m"
useJUnitPlatform {
filter {
includeTags("integration-test")
}
}
setTestNameIncludePatterns(listOf("*${testName}"))
}
}
如果我运行,一切都按预期工作。./gradlew -PtestName="MyTest" :project1:integration-test-named
如果我运行,我会收到错误,因为 gradle 会查找 MyTest。我需要指定测试所在的子项目才能使其正常工作。./gradlew -PtestName="MyTest" integration-test-named
project2
是否可以不需要指定特定测试所在的项目?
答:
0赞
Tyler DeWitt
11/17/2023
#1
一个朋友能够帮助我。您可以设置
setFailOnNoMatchingTests(false)
作为 JUnit 配置器的一个选项。
所以在 Kotlin 中:
fun TaskContainerScope.integrationTestsNamedTask(taskName: String) {
val testName = project.property("testName") as String
println(testName)
register<Test>(taskName) {
description = "Run specific integration test"
shouldRunAfter("test")
minHeapSize = "512m"
maxHeapSize = "1024m"
useJUnitPlatform {
filter {
includeTags("integration-test")
isFailOnNoMatchingTests = false
}
}
setTestNameIncludePatterns(listOf("*${testName}"))
}
}
请注意
isFailOnNoMatchingTests = false
评论