gpt4 book ai didi

java - 如何通过 Kotlin Gradle 和 -D 为我的测试提供系统属性

转载 作者:行者123 更新时间:2023-11-30 07:41:02 25 4
gpt4 key购买 nike

当我在 Gradle 中运行测试时,我想传递一些属性:

./gradlew test -DmyProperty=someValue

所以在我的 Spock 测试中,我将使用检索值:

def value = System.getProperty("myProperty")

我正在使用 kotlin gradle dsl。当我尝试使用本文档中的“tasks.test”时: https://docs.gradle.org/current/userguide/java_testing.html#test_filtering

'test' 在我的 build.gradle.kts 文件中无法识别。

我假设我需要使用类似于下面帖子中的答案的东西,但不清楚在使用 gradle kotlin DSL 时应该如何完成。

How to give System property to my test via Gradle and -D

最佳答案

您的链接问题的答案可以 1:1 翻译成 kotlin DSL。这是使用 junit5 的完整示例。

dependencies {
// ...
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation(kotlin("test-junit5"))
}

tasks.withType<Test> {
useJUnitPlatform()

// Project property style - optional property.
// ./gradlew test -Pcassandra.ip=xx.xx.xx.xx
systemProperty("cassandra.ip", project.properties["cassandra.ip"])

// Project property style - enforced property.
// The build will fail if the project property is not defined.
// ./gradlew test -Pcassandra.ip=xx.xx.xx.xx
systemProperty("cassandra.ip", project.property("cassandra.ip"))

// system property style
// ./gradlew test -Dcassandra.ip=xx.xx.xx.xx
systemProperty("cassandra.ip", System.getProperty("cassandra.ip"))
}

关于java - 如何通过 Kotlin Gradle 和 -D 为我的测试提供系统属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com