如何以智能方式将系统属性传递给 gradle 中的测试?

How to pass system properties to the tests in gradle in the smart way?

build.gradle

tasks.withType(Test){
    systemProperties=System.properties
    println systemProperties['param']
}

现在我可以在命令行中传递参数:

gradle test -Dparam=10

或将它们放在 gradle.properties:

systemProp.param=15

理想情况下,我想将默认值放在 gradle.properties 中,并能够从命令行覆盖它们。不幸的是,如果我这样做,gradle.properties 优先,-Dparam=10 被忽略。

你能提供任何解决方案吗?

https://issues.gradle.org/browse/GRADLE-2122

从 2.12 或 2.13 "the smart way" 开始就可以使用了!

上面的例子有效,命令行 -D 选项覆盖了 gradle.properties

中的默认值

我正在使用 gradle 2.12 并分享我的使用方法:

test {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}

我想跳过 JUnit 测试,除非使用 属性 来包含此类测试。使用 JUnit Assume 有条件地包含测试:

//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)

使用 gradle 执行此操作需要 属性 在 运行 gradle 构建时提供的系统,如此处所示,

gradle build -Ddeep.test.run=true

确实通过了测试

希望这可以帮助其他人有条件地尝试这种方法进行 运行 测试。