【发布时间】:2013-05-10 11:12:13
【问题描述】:
我需要确定我的 Grails 应用程序当前是否正在测试中。我不能使用if (Environment.getCurrent() == Environment.TEST),因为我当前的环境是Environment.CUSTOM,名称为jenkins。是否有其他方法可以确定它当前是否正在测试中?
【问题讨论】:
我需要确定我的 Grails 应用程序当前是否正在测试中。我不能使用if (Environment.getCurrent() == Environment.TEST),因为我当前的环境是Environment.CUSTOM,名称为jenkins。是否有其他方法可以确定它当前是否正在测试中?
【问题讨论】:
我使用的一种方法是通过挂钩eventTestPhaseStart GANT 事件来设置环境变量。您可以通过在/scripts 中创建一个名为Events.groovy 的脚本来做到这一点。
<project dir>/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."
【讨论】: