【问题标题】:How to pass system properties to the tests in gradle in the smart way?如何以智能的方式将系统属性传递给 gradle 中的测试?
【发布时间】:2016-11-02 17:21:44
【问题描述】:

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 将被忽略。

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

【问题讨论】:

  • discuss.gradle.org/t/… 似乎 gradle.properties 中定义的系统属性目前无法使用 -D 覆盖
  • 谢谢,将 gradle wrapper 更新到最新,解决了问题。

标签: gradle system-properties


【解决方案1】:

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

它从 2.12 或 2.13 开始就可以“聪明地”使用了!

上面的例子正在运行,命令行 -D 选项超过了 gradle.properties

中的默认值

【讨论】:

    【解决方案2】:

    我正在使用 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 build 时提供的系统属性,如下所示,

    gradle build -Ddeep.test.run=true
    

    确实通过了测试。

    希望这有助于其他人尝试这种方法来有条件地运行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2016-11-08
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2017-02-10
      相关资源
      最近更新 更多