Using the OSX Console.app to aid Grails Test Driven Development

2008/03/13

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.

Grails test-app log 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.

Filtering out lines in Console.app

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 Console.app

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

There are 5 comments in this article:

  1. 2008/03/13Indy Nagpal say:

    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.

  2. 2008/03/13Mike say:

    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!

  3. 2008/03/15James Deville say:

    This is an awesome tip for general logs. I’ll have to checkout console more!

  4. 2008/04/22Matthew Taylor say:

    Great tip! I started using this immediately.

  5. 2009/06/14Ted Naleid » Grails Testing Command Line Aliases say:

    [...] These aliases are all set up to run the “testlog” alias if a failing exit code is returned from the test run. This searched 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. [...]

Write a comment: