Mar 13th 2008 05:24 am Using the OSX Console.app to aid Grails Test Driven Development
With Groovy built-in, Grails is one of the best platforms running on the JVM for doing unit testing.
One limitation that it does have is that the current testing scripts seem to be oriented more towards creating test reports rather than giving the user the quick feedback that is helpful when doing Test Driven Development.
Here’s an example of the output that you get on a failed test after running “grails test-app”:
------------------------------------------------------- Running 1 Integration Test... Running test BookTests... testBookHasExpectedTitle...FAILURE Integration Tests Completed in 622ms ------------------------------------------------------- [junitreport] Transform time: 944ms Tests failed: 0 errors, 1 failures, 0 compilation errors. View reports in /Users/ted/Documents/workspace/foobar/test/reports
Ok, that’s great…a test failed. But why? Where is the stacktrace and any output generated during the test?
As the output says, its down under test/reports. Once you get there, you see that there are a number of different output files and formats, everything from xml files, standard error/out files, as well as plain output files.
Even for the tests that pass, Grails still puts an output file in the reports directory for it. When you start to accumulate a number of different unit tests, it can be a pain to find the right output file and load it up in something that lets you quickly figure out the source of the problem in that long, groovy stacktrace.
Console.app and Aliases to the Rescue
OSX comes with a utility called Console.app that is normally used to look at system logs. It turns out that it’s also an excellent tool for looking at the output logs of Grails and for monitoring testing output when doing TDD.
OSX also allows you to launch any file (with the default file handler) from the command line with the “open” command. If you use the “-a” switch, you can pass it the name of an application
Combining that with a little grep magic and a for loop allows us to identify all of the test output logs that actually had an error or a failure:
alias oel='for F in `grep -lE "FAILED|ERROR" test/reports/plain/*.txt`; do echo ">>> opening $F with Console"; open -a Console.app $F; done;'
If you stick that in your .profile all you have to after running “grails test-app” is type “oel” and any failing tests will automatically pop open in Console.app.

Once the console is open, you can use cmd-K to clear the console and shift-cmd-R to reload it. The filtering abilities are also quite nice for finding relevant lines within the huge groovy stacktraces. Just type the name of your class or package in the filter box (opt-shift-F) and it will only show you the lines that match your filter.

Alternatively, I like to switch from filtering lines out to highlighting the filter text. This way, I can still see the surrounding stacktrace lines, but it’s easy to find the lines I actually care about.

My days of using tail and less are over, but if you’re on a non-OSX platform, you can easily substitute one of these commands in the alias that you execute (if you’re on Windows, check out cygwin).
Posted by tednaleid / grails and groovy and osx and unit testing
Indy Nagpal on 13 Mar 2008 at 8:48 am #
Ah. A very useful blog post. Thanks!
I love Console! I use it all the time to trace Flash log as well as ColdFusion log files. I use it for custom log files. But didn’t know one could setup alias like this.
Will try it out soon.
Mike on 13 Mar 2008 at 11:09 am #
Nice trick, Ted! I currently use an alias to open the reports directory using TextMate and then do a “Find” to track down the failed test(s). This will make it much easier for me to get to the information I need when a test fails!
James Deville on 15 Mar 2008 at 7:20 am #
This is an awesome tip for general logs. I’ll have to checkout console more!
Matthew Taylor on 22 Apr 2008 at 11:12 am #
Great tip! I started using this immediately.