我需要确定我的 Grails 应用程序当前是否正在测试中。我不能使用 if (Environment.getCurrent() == Environment.TEST)
,因为我当前的环境是 Environment.CUSTOM
,名称为 jenkins。有没有其他方法可以查明它目前是否正在测试中?
最佳答案
我使用的一种方法是通过连接到 eventTestPhaseStart
来设置环境变量。甘特事件。您可以通过创建名为 Events.groovy
的脚本来完成此操作在/scripts
.
<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."
关于testing - 我如何确定 Grails 应用程序是否在测试中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16480975/