Grails Testing Command Line Aliases

| Comments

I’ve recently thrown together a few different command line aliases that have been very helpful in my grails development and I thought others might benefit from them.

The aliases are primarily just the first letter of the words in the command, which makes them easy to remember (and saves on typing). Some examples:

gta                   # grails test-app
gtai                  # grails test-app -integration
gtaud AuthorService   # grails-debug-suspend test-app -unit AuthorService  

My testing aliases are probably my most often used grails aliases. They’re all “gta” (for grails test-app) followed by an optional “u” or “i” (to run only unit or integration).

There’s also an alternate for each of those commands with a “d” on the end for “debug”. I call a script named “grails-debug-suspend” which is simply a copy of the normal “grails-debug” command, but with the “suspend” flag set to “y”. This causes the application to stop right away till you attach a remote debugger. This is very useful for quick tests as its often the case that the code gets past where you have breakpoints set before you launch the debugger.

These commands all also let you pass in the name of a specific test class so that only that test runs.

The overall format for all of the aliases is:

gta[i|u][d] [TestClassName]

This example suspends until a debugger is attached and then runs the AuthorServiceTests.groovy unit test class:

gtaud AuthorService

These aliases are all set up to run the “testlog” alias if a failing exit code is returned from the test run. This searches through all of the testlog results for any failures and automatically opens up each failing testlog in the OSX (if you’re on another platform, just substitute a different editor). I wrote a more detailed post a while ago about how nice is for looking at grails test logs.

Lastly, each command is also run with the unix “time” command, which I find useful to help me see how long my tests are taking and get a feel over time for the overall performance of the system under test.

# grails-debug-suspend doesn't exist by default, it's a copy of grails-debug with the suspend flag changed to "y" so that
# we can attach a remote debugger before it proceeds past startup

alias gta=grailsTestApp
function grailsTestApp() { grailsTest grails "" $1 }

alias gtad=grailsTestAppDebug
function grailsTestAppDebug() { grailsTest grails-debug-suspend "" $1 }

alias gtau=grailsTestAppUnit
function grailsTestAppUnit() { grailsTest grails -unit $1 }

alias gtaud=grailsTestAppUnitDebug
function grailsTestAppUnitDebug() { grailsTest grails-debug-suspend -unit $1 }

alias gtai=grailsTestAppIntegration
function grailsTestAppIntegration() { grailsTest grails -integration $1 }

alias gtaid=grailsTestAppIntegrationDebug
function grailsTestAppIntegrationDebug() { grailsTest grails-debug-suspend -integration $1 }

function grailsTest() {
    echo "Running: $1 test-app $2 $3 || testlog "
    time $1 test-app $2 $3 || testlog

# after grails test if there were ERROR messages, you can open those logs with the using this
alias testlog='for F in `grep -lE "FAILED|Caused\ an\ ERROR" test/reports/plain/*.txt`; do echo ">>> opening" $F; open -a Console $F; done;'

You can place these aliases in your .bashrc/.bash_profile in your home directory (you can also use them with cygwin on windows.

Alternatively, if you’re using zsh (or interested in switching from bash), I’ve got all of my zsh setup shared in a source control repo in a format that’s easy to use and extend for yourself.

This is the modified grails-debug command called grails-debug-suspend, just put it somewhere in your PATH and make it executable (chmod +x grails-debug-suspend):


. "$DIRNAME/startGrails"

JAVA_OPTS="$JAVA_OPTS -Xdebug -Xnoagent -Dgrails.full.stacktrace=true -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
startGrails org.codehaus.groovy.grails.cli.GrailsScriptRunner "$@"