Using the OSX to Aid Grails Test Driven Development

| Comments

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...
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. and Aliases to the Rescue

OSX comes with a utility called 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 $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

Grails test-app log in

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.

Filtering out lines in

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.

Highlighting lines in

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).