Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 179 lines (151 sloc) 6.164 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.codehaus.groovy.grails.cli.fork.testing.ForkedGrailsTestRunner
import org.codehaus.groovy.grails.support.PersistenceContextInterceptorExecutor
import org.codehaus.groovy.grails.test.runner.GrailsProjectTestRunner
import org.codehaus.groovy.grails.test.runner.phase.FunctionalTestPhaseConfigurer
import org.codehaus.groovy.grails.test.runner.phase.IntegrationTestPhaseConfigurer

/**
* Gant script that runs the Grails unit tests
*
* @author Graeme Rocher
*
* @since 0.4
*/

includeTargets << grailsScript("_GrailsBootstrap")
includeTargets << grailsScript("_GrailsRun")
includeTargets << grailsScript("_GrailsClean")

projectTestRunner = new GrailsProjectTestRunner(projectPackager)
projectTestRunner.testExecutionContext = binding
// Miscellaneous 'switches' that affect test operation
testOptions = projectTestRunner.testOptions

// The four test phases that we can run.
unitTests = projectTestRunner.testFeatureDiscovery.unitTests
integrationTests = projectTestRunner.testFeatureDiscovery.integrationTests
functionalTests = projectTestRunner.testFeatureDiscovery.functionalTests
otherTests = projectTestRunner.testFeatureDiscovery.otherTests

// The potential phases for execution, modify this by responding to the TestPhasesStart event
phasesToRun = projectTestRunner.phasesToRun

TEST_PHASE_WILDCARD = GrailsProjectTestRunner.TEST_PHASE_WILDCARD
TEST_TYPE_WILDCARD = GrailsProjectTestRunner.TEST_TYPE_WILDCARD

targetPhasesAndTypes = projectTestRunner.targetPhasesAndTypes // Passed to the test runners to facilitate event publishing
testEventPublisher = projectTestRunner.testEventPublisher // Add a listener to generate our JUnit reports.

// A list of test names. These can be of any of this forms:
//
// org.example.*
// org.example.**.*
// MyService
// MyService.testSomeMethod
// org.example.other.MyService.testSomeMethod
//
// The default pattern runs all tests.
testNames = projectTestRunner.testNames

// Controls which result formats are generated. By default both XML
// and plain text files are created. You can override this in your
// own scripts.
reportFormats = projectTestRunner.reportFormats

// If true, only run the tests that failed before.
reRunTests = projectTestRunner.reRunTests

// Where the report files are created.
testReportsDir = grailsSettings.testReportsDir
// Where the test source can be found
testSourceDir = grailsSettings.testSourceDir

junitReportStyleDir = projectTestRunner.junitReportStyleDir

createTestReports = true

testsFailed = false
projectWatcher = null

target(allTests: "Runs the project's tests.") {
    depends(compile, startLogging, packagePlugins, configureServerContextPath)
    Integer exitCode
    if(grailsSettings.forkSettings.test) {
        if(argsMap?.war) {
            projectRunner.warCreator.packageWar()
        }
        def forkedTestRunner = new ForkedGrailsTestRunner(grailsSettings)
        if(grailsSettings.forkSettings.test instanceof Map) {
            forkedTestRunner.configure(grailsSettings.forkSettings.test)
        }
        exitCode = forkedTestRunner.fork(argsMap)?.waitFor()
        if(exitCode != null) {
            exit(exitCode)
        }
    }
    else {
        exitCode = projectTestRunner.runAllTests(argsMap) ? 0 : 1
    }
}

/**
* Compiles and runs all the tests of the given type and then generates
* the reports for them.
* @param type The type of the tests to compile (not the test phase!)
* For example, "unit", "jsunit", "webtest", etc.
*/
processTests = projectTestRunner.&processTests

/**
* Compiles all the test classes for a particular type of test, for
* example "unit" or "webtest". Assumes that the source files are in
* the "test/$type" directory. It also compiles the files to distinct
* directories for each test type: "$testClassesDir/$type".
* @param type The type of the tests to compile (not the test phase!)
* For example, "unit", "jsunit", "webtest", etc.
*/
compileTests = projectTestRunner.projectTestCompiler.&compileTests

runTests = projectTestRunner.&runTests

initPersistenceContext = {
    if (appCtx != null) {
        PersistenceContextInterceptorExecutor.initPersistenceContext(appCtx)
    }
}

destroyPersistenceContext = {
    if (binding.variables.containsKey("appCtx") && appCtx != null) {
        PersistenceContextInterceptorExecutor.destroyPersistenceContext(appCtx)
    }
}

unitTestPhasePreparation = {}
unitTestPhaseCleanUp = {}

/**
* Initialises a persistence context and bootstraps the application.
*/
def integrationPhaseConfigurer = new IntegrationTestPhaseConfigurer(projectTestRunner.projectTestCompiler, projectLoader)
projectTestRunner.testFeatureDiscovery.configurers.integration = integrationPhaseConfigurer
integrationTestPhasePreparation = {
    integrationPhaseConfigurer.prepare()
}

/**
* Shuts down the bootstrapped Grails application.
*/
integrationTestPhaseCleanUp = {
    integrationPhaseConfigurer.cleanup()
}

def functionalPhaseConfigurer = new FunctionalTestPhaseConfigurer(projectRunner)
projectTestRunner.testFeatureDiscovery.configurers.functional = functionalPhaseConfigurer
/**
* Starts up the test server.
*/
functionalTestPhasePreparation = {
    functionalPhaseConfigurer.prepare()
}

/**
* Shuts down the test server.
*/
functionalTestPhaseCleanUp = {
    functionalPhaseConfigurer.cleanup()
}

otherTestPhasePreparation = {}
otherTestPhaseCleanUp = {}

target(packageTests: "Puts some useful things on the classpath for integration tests.") {
    projectTestRunner.projectTestCompiler.packageTests(false)
}
Something went wrong with that request. Please try again.