Skip to content

Commit 5320c5a

Browse files
ldaleypledbrook
authored andcommitted
GRAILS-6690,GRAILS-6643,GRAILS-6644 - improve the flexibility of functional testing.
This incorporates: • Running the tests against a WAR • Running the tests against an external server (avoids grails startup cost) • Providing the “base url” as a build variable
1 parent 8681838 commit 5320c5a

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

scripts/_GrailsTest.groovy

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.codehaus.groovy.grails.test.event.GrailsTestEventConsoleReporter
4040

4141
includeTargets << grailsScript("_GrailsBootstrap")
4242
includeTargets << grailsScript("_GrailsRun")
43+
includeTargets << grailsScript("_GrailsWar")
4344
includeTargets << grailsScript("_GrailsSettings")
4445
includeTargets << grailsScript("_GrailsClean")
4546

@@ -130,11 +131,11 @@ target(allTests: "Runs the project's tests.") {
130131
convertedPhases[phaseName] = types.collect { rawType ->
131132
if (rawType instanceof CharSequence) {
132133
def rawTypeString = rawType.toString()
133-
if (phaseName in ['integration', 'functional']) {
134+
if (phaseName == 'integration') {
134135
def mode = new JUnit3GrailsTestTypeMode(
135136
autowire: true,
136-
wrapInTransaction: phaseName == "integration",
137-
wrapInRequestEnvironment: phaseName == "integration"
137+
wrapInTransaction: true,
138+
wrapInRequestEnvironment: true
138139
)
139140
new JUnit3GrailsTestType(rawTypeString, rawTypeString, mode)
140141
} else {
@@ -334,25 +335,51 @@ integrationTestPhaseCleanUp = {
334335
* Starts up the test server.
335336
*/
336337
functionalTestPhasePreparation = {
337-
packageApp()
338-
testOptions.https ? runAppHttps() : runApp()
338+
runningFunctionalTestsInline = !testOptions.containsKey('baseUrl') || testOptions.inline
339+
runningFunctionalTestsAgainstWar = testOptions.war
340+
341+
if (runningFunctionalTestsAgainstWar) {
342+
// need to swap out the args map so any test phase/targetting patterns
343+
// aren't intepreted as the war name.
344+
def realArgsMap = argsMap
345+
argsMap = [:]
346+
war()
347+
argsMap = realArgsMap
348+
349+
testOptions.https ? runWarHttps() : runWar()
350+
} else if (runningFunctionalTestsInline) {
351+
packageApp()
352+
testOptions.https ? runAppHttps() : runApp()
353+
prevAppCtx = binding.hasProperty('appCtx') ? appCtx : null
354+
appCtx = ApplicationHolder.application.mainContext
355+
initPersistenceContext()
356+
}
339357

340-
prevAppCtx = binding.hasProperty('appCtx') ? appCtx : null
341-
appCtx = ApplicationHolder.application.mainContext
358+
if (testOptions.containsKey('baseUrl')) {
359+
functionalBaseUrl = testOptions.baseUrl
360+
} else {
361+
functionalBaseUrl = (testOptions.httpsBaseUrl ? 'https' : 'http') + "://localhost:$serverPort$serverContextPath/"
362+
}
342363

343-
initPersistenceContext()
364+
System.setProperty(grailsSettings.FUNCTIONAL_BASE_URL_PROPERTY, functionalBaseUrl)
344365
}
345366

346367
/**
347368
* Shuts down the test server.
348369
*/
349370
functionalTestPhaseCleanUp = {
350-
destroyPersistenceContext()
351-
352-
appCtx?.close()
353-
appCtx = prevAppCtx
371+
if (runningFunctionalTestsInline) {
372+
destroyPersistenceContext()
373+
appCtx?.close()
374+
appCtx = prevAppCtx
375+
}
376+
377+
if (runningFunctionalTestsInline || runningFunctionalTestsAgainstWar) {
378+
stopServer()
379+
}
354380

355-
stopServer()
381+
functionalBaseUrl = null
382+
System.setProperty(grailsSettings.FUNCTIONAL_BASE_URL_PROPERTY, '')
356383
}
357384

358385
otherTestPhasePreparation = {}

src/java/grails/util/BuildSettings.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ class BuildSettings {
125125
*/
126126
public static final String VERBOSE_COMPILE = "grails.project.compile.verbose"
127127

128+
/**
129+
* A system property with this name is populated in the preparation phase of functional testing
130+
* with the base URL that tests should be run against.
131+
*/
132+
public static final String FUNCTIONAL_BASE_URL_PROPERTY = 'grails.testing.functional.baseUrl'
133+
134+
128135
/**
129136
* The base directory for the build, which is normally the root
130137
* directory of the current project. If a command is run outside
@@ -1032,4 +1039,8 @@ class BuildSettings {
10321039
this.webXmlLocation = location
10331040
this.webXmlFileSet=true
10341041
}
1042+
1043+
String getFunctionalTestBaseUrl() {
1044+
System.getProperty(FUNCTIONAL_BASE_URL_PROPERTY)
1045+
}
10351046
}

0 commit comments

Comments
 (0)