Tuesday, June 2, 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 !!!

BEAUTY !!!

7 comments:

  1. Beauty Indeed! Thanks for the tip, just what I was looking for. Although grails and netbeans are both fab, I'm struggling to get maximum productivity using the 2 together - eg refactoring!

    Thanks again

    ReplyDelete
  2. Indeed, after working with Grails for a while, I do miss the safety of a statically typed language like Java . Every once in a while I do something stupid that a compiler would have caught, I bang my head against the wall, and when I figure out the problem I miss Java as the compiler would have definitely caught it.

    Also, on Grails and NetBeans... I'm still having issues w/ performance, even w/ NB 6.8 - the project scanning just runs forever !

    ReplyDelete
  3. Thanks much for this contribution. I will vote this issue up too.

    ReplyDelete
  4. Any clues how to debug a single test (vs . the entire class ?)

    ReplyDelete
  5. set the breakpoint first, then open the run grails command window under netbeans 6.8+ and click the debug box at the left bottom corner of the window, then do the same way running the single unit test mentioned above. You can see the debugging running successfully. :)

    ReplyDelete
  6. In order to run/debug a single test, according to http://jira.codehaus.org/browse/GRAILS-2473, after Grails 1.3.2 it is now possible to run "grails test-app -unit Foo.testFoo" in order to run/debug the unit tests in class FooTests.testFoo . In other words, if you follow the instructions above, typing "test-app" in the filter, and specifying "-unit Foo.testFoo" in the parameters box.

    ReplyDelete

Popular Posts

Related Posts with Thumbnails