<< October 2013 | Home | December 2013 >>

Adding a custom test phase to Grails 2.3

Here's a snippit showing how to add a custom test phase and to exclude other phases, this code is pretty raw but should help you in figuring things out. The way that test phases are created has changed alot since grails 1.3 and the grails documentation doesn't show how to do it. Also as of November 2013 Luke Daley's blog post on the subject from the grails 1.3.7 days has disappeared.

scripts/_Events.groovy

buildRawGrailsEnv = {
    return System.getProperties().get('grails.env').toString().toLowerCase()
}

eventTestPhasesStart = { phasesToRun ->

    // background: in our codebase we have an environment called 'remote' and the only valid phases for that environment is the new 'remote' phase.

    // if the environment is not 'remote' then do not add the new phase.
    String rawGrailsEnv = buildRawGrailsEnv()
    if (!rawGrailsEnv.equals('remote')) {
        return
    }

    // exclude all other phases except the new 'remote' phase.
    def phasesToExclude = phasesToRun.collect { return it }
    phasesToRun << "remote"
    phasesToExclude.each {
        phasesToRun.remove(it)
    }

    def remotePhaseConfigurer = new IntegrationTestPhaseConfigurer(projectTestRunner.projectTestCompiler, projectLoader)
    projectTestRunner.testFeatureDiscovery.configurers.remote = remotePhaseConfigurer

    def remoteTestTypeName = "remote"
    def remoteTestDirectory = "remote"
    def remoteTestMode = new GrailsTestMode(autowire: true, wrapInTransaction: true, wrapInRequestEnvironment: true)
    def remoteTestType = new GrailsSpecTestType(remoteTestTypeName, remoteTestDirectory, remoteTestMode)

    projectTestRunner.testFeatureDiscovery.testExecutionContext.remoteTests = [remoteTestType]
}

remoteTestPhasePreparation = {
    // called at the start of the phase
    println "*** Starting remote tests"
    integrationTestPhasePreparation()
}

remoteTestPhaseCleanUp = {
    // called at the end of the phase
    println "*** Finished remote tests"
    integrationTestPhaseCleanUp()
}