Tuesday, June 02, 2009

Quickly running a single Grails test in NetBeans

One thing in the current NetBeans Grails project is that out of the box, you can run all tests in the project from the project's context menu but you can't run individual tests. While that is all good and well when you only have a few tests, once the tests are piling up (or you just are interested in how a particular test works), it is much preferable to be able to execute a single test at a time.

There is an existing issue to address this (so, go and vote for the issue if it seems useful, would ya?), but in the meantime, you still need to run your individual tests, right ? So, what should you do ?

The trick is to just use the "Run Grails Command" feature in NetBeans and make it easily accessible so that you can run the test in a heartbeat.

Here's what the feature looks like:

  • Right click on the project and select the "Run Grails Command..." option

  • In the dialog, type "test-app" in the Filter text box, and then type in "TestName -unit" for unit tests, or "TestName -integration" for integration tests. Note that here "TestName" is the test class name minus the "Tests" suffix for unit tests and minus the "IntegrationTests" for integration tests.

The good news is that NetBeans remembers the command options from the previous run (not the command though) and if you pull up the dialog you just have to type "test-app" and hit enter and it will execute your previous test class.

Thus, the only thing you have left it to make it easy to call the "Execute Grails Command" - so, we just need to map a shortcut. In order to do that, go to Tools->Options->Key Map tab. In the "Search" box, type "grails", the "Execute Grails Command" action is there, click in the Shortcut column, type in your shortcut (I picked Ctrl+Alt-C) and hit OK.

Now, running your test the first time involves Ctrl-Alt-C, type "test-app", tab, type "FooBar -unit" and hitting enter. Kicking it off subsequently is Ctrl-Alt-C, type "test-app", hit enter and your tests are running !!!