Testing 如何确定Grails应用程序是否正在测试中?
我需要确定我的Grails应用程序当前是否正在测试中。如果(Environment.getCurrent()==Environment.TEST),我不能使用Testing 如何确定Grails应用程序是否正在测试中?,testing,grails,spock,Testing,Grails,Spock,我需要确定我的Grails应用程序当前是否正在测试中。如果(Environment.getCurrent()==Environment.TEST),我不能使用,因为我当前的环境是名为jenkins的Environment.CUSTOM。有没有其他方法可以确定它当前是否正在测试中?我使用的一种方法是通过连接到事件testphaseStart甘特事件来设置环境变量。您可以通过在/scripts中创建名为Events.groovy的脚本来实现这一点 /scripts/Events.groovy: ev
,因为我当前的环境是名为jenkins的Environment.CUSTOM
。有没有其他方法可以确定它当前是否正在测试中?我使用的一种方法是通过连接到事件testphaseStart
甘特事件来设置环境变量。您可以通过在/scripts
中创建名为Events.groovy
的脚本来实现这一点
/scripts/Events.groovy
:
eventTestPhaseStart = { args ->
System.properties['grails.testPhase'] = args
}
您可以这样使用它来确定应用程序是否正在测试中:
if (System.properties['grails.testPhase'] != null)
println "I'm testing!"
您还可以使用它为特定测试阶段提供特定行为:
if (System.properties['grails.testPhase'] == 'integration')
println "Do something only when running integration tests."
谢谢,这是我一直在寻找的方法。我希望已经有了某种内在的东西。