Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

How can I determine if Grails application under testing?

I need to determine if my Grails application is currently under testing. I cannot use if (Environment.getCurrent() == Environment.TEST), because my current environment is Environment.CUSTOM with name jenkins. Is there an other way to find out if it's currently under testing?

Upvotes: 0

Views: 219

Answers (1)

codelark
codelark

Reputation: 12334

One approach I've used is to set an environment variable by hooking into the eventTestPhaseStart GANT event. You can do this by creating a script named Events.groovy in /scripts.

<project dir>/scripts/Events.groovy:

eventTestPhaseStart = { args ->
    System.properties['grails.testPhase'] = args
}

You can use it like so to determine if the app is under testing:

if (System.properties['grails.testPhase'] != null)
    println "I'm testing!"

You can also use it to have specific behavior for a particular test phase:

if (System.properties['grails.testPhase'] == 'integration')
    println "Do something only when running integration tests."

Upvotes: 2

Related Questions