4

这应该是最简单的问题,但我在从 Gradle 测试任务访问系统变量时遇到问题。我所做的事情一定有错字,因为我确信这种语法是正确的,但它不起作用。我希望有人可以帮助我确定下面这段代码片段的问题?

// my gradle build says the following line is a deprecated method    
//systemProperties = System.getProperties()

// this line always returns 1 on a multiprocessor system
println 'NUMBER_OF_PROCESSORS is ' +
     System.getProperty( "NUMBER_OF_PROCESSORS", "1" )

// this line also always returns the default for TMP var
println 'TMP is ' + System.getProperty( "TMP", "C:\\Temp" )

注意:我也在这里问过这个问题,但由于它是一个封闭的线程,我不确定我是否会在那里得到答案。另外,我已经彻底阅读了文档,但没有帮助。

我尝试了这些,但它们也失败了:

test {
    println ""
    ENV = System.getProperties()
    println "TMP is " + ENV['TMP']
    println ""
}

task testa(Type:Test) { 
    println ""
    println "HOMEPATH = " + System.getProperty( "HOMEPATH", "defaultpath" )
    println "TMP = " + System.getProperty( "TMP", "defaulttmp" )
    println ""
}

task testb(Type:Test) {
    println ""
    println "HOMEPATH = " + System.properties['HOMEPATH']
    println "TMP = " + System.properties['TMP']
    println ""
}

task testc(Type:Test) {
    // pass a arg to this test like -PMYARG=anything
    println ""
    println "Parg = " + System.properties['MYARG']
    println ""
}

testWorks {
    println ""
    ENV['ok'] = "good to go"
    println "this test is " + ENV['ok']
    println ""
}
4

1 回答 1

9

要使 Gradle JVM 的系统属性可用于测试,您可以执行以下操作:

test {
    systemProperties = System.properties
}

或者,如果您声明了其他Test任务:

tasks.withType(Test) {
    systemProperties = System.properties
}
于 2013-04-20T00:25:10.413 回答