【问题标题】:Gradle pass SystemProperties to JUnit testGradle 将 SystemProperties 传递给 JUnit 测试
【发布时间】:2017-01-14 18:07:54
【问题描述】:

如何在JUnit 测试用例中使用gradle parameters

我试过了:

build.bradle:

test {
    systemProperty 'brand', System.properties['brand'] ? project.brand : 'abc'
}

在我的测试中,我显示了所有属性:

System.properties.each { k,v->
    log.info("$k = $v")
}

使用该代码,我得到了很多属性,brand 除外。

还有其他方法可以让品牌进入我的测试吗?

// 我不知道它是否重要,但我也在使用 Gebish 或 Selenium 进行测试。

【问题讨论】:

    标签: gradle groovy junit properties


    【解决方案1】:

    不知道为什么在test 配置中使用systemProperty 不起作用,但您可以设置systemProperties 变量,如:

    test {
        System.properties.'brand' = System.properties['brand'] ? project.brand : 'abc'
        systemProperties = System.properties
    }
    

    或者不想修改System.properties可以设置systemPropertieslike

    test {
        systemProperties = System.properties + ['brand1': System.properties['brand'] ? project.brand : 'abc']
    

    }

    【讨论】:

    • jUnit 为每个测试使用一个新的 JVM。 Gebish也不知何故。你的想法行不通。但我有一个丑陋的解决方法。在我的 build.gradle 中,我在测试任务中有 systemProperty "brand", project.hasProperty("brand") ? project.brand : "abc"。在我的 GebConfig.groovy 中,我有 brand = System.getProperty("brand") ? System.getProperty("brand") : "abc"(这里又是 abc,因为 Intellij 没有正确运行任务),在我的测试中我有 def brand = browser.config.getRawConfig().getProperty("brand").toString()。我现在花了 4 个多小时才找到这条路
    • systemProperties 应该在新的 JVM 中设置 System.properties 在 Geb 和 Junit 测试中,我正在执行上述操作。你确定你设置了 systemProperties 变量吗?
    • 是的,我确定。我刚刚又试了一次,System.properties(在测试中)没有品牌密钥。
    【解决方案2】:

    我现在的解决方案是:

    build.gradle(在测试任务中):

    systemProperty "brand", project.hasProperty("brand") ? project.brand : "abc" 
    

    GebConfig.groovy:

    brand = System.getProperty("brand") ? System.getProperty("brand") : "abc"
    

    这里又是 abc,因为 IntelliJ 没有正确运行任务,而是通过命令行运行的

    在测试本身中:

    def brand = browser.config.getRawConfig().getProperty("brand").toString(‌​)
    

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多