Grails Testing Command Line Aliases

2009/06/14

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 Console.app (if you’re on another platform, just substitute a different editor). I wrote a more detailed post a while ago about how nice Console.app 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 Console.app 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):

#!/bin/bash
 
DIRNAME=$GRAILS_HOME/bin
. "$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 "$@"

There are 5 comments in this article:

  1. 2009/08/23John Kivus say:

    I’m trying to use your aliases on Mac OS X 10.5, and I’m having issues when pasting them into my .bash_profile. I get the generic .bash_profile EOF line error if I paste them all…or even if I paste just one and then the final two functions. I know you don’t want to be debugging people’s problems, but are there any tricks I should try?

  2. 2009/08/24tednaleid say:

    @John

    Hmm…I haven’t seen that issue before. It’s possible it’s the editor you’re using? You could edit the file in textedit to see if that does it (just type “open -e .bash_profile” on the command line.

    If that doesn’t work, it could be the way things are getting copied out of the browser, these same aliases are in my full zshrc files that I’ve got on bitbucket (towards the bottom). You could try to download them from there by clicking on the “raw” link and see if cutting/pasting from that works.

    Another thing you could try would be to add it to a .bashrc rather than a .bash_profile. I’m a little rusty on my bash, so I’m not sure if it’ll load both or if the .bashrc will override.

    Worst case, I’d just try to retype one of the aliases and the related functions, if that works, retype it all. I did it once and it wasn’t that bad :)

  3. 2009/08/24John Kivus say:

    @tednaleid

    Thanks for the tips. I was playing around with retyping them in TextEdit, going line by line and I figured out one of those “it works, but I have no idea why solutions”. If I format the alias / function combination like this:

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

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

    then things work fine. Like I said, no idea why my my Mac is happy with that, but not in the other format, but at least they’re all working. Thank for the help and the also the great aliases.

  4. 2009/08/24tednaleid say:

    @John Interesting, thanks for posting your solution back. It could be that bash doesn’t like the one liners. I use a mac but also use zsh instead of bash so that could be something that zsh thinks is ok.

  5. 2009/11/3Ted Naleid » Grails Testing Alias to Rerun Failed Tests say:

    [...] while ago I blogged about my grails testing aliases and how much time they save [...]

Write a comment: