【问题标题】:How can I determine if Grails application under testing?如何确定 Grails 应用程序是否在测试中?
【发布时间】:2013-05-10 11:12:13
【问题描述】:

我需要确定我的 Grails 应用程序当前是否正在测试中。我不能使用if (Environment.getCurrent() == Environment.TEST),因为我当前的环境是Environment.CUSTOM,名称为jenkins。是否有其他方法可以确定它当前是否正在测试中?

【问题讨论】:

    标签: testing grails spock


    【解决方案1】:

    我使用的一种方法是通过挂钩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."
    

    【讨论】:

    • 谢谢,这是我一直在寻找的方法。我希望已经内置了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多