Friday, December 30, 2011

ScalaTest, SBT, debugging mode and IDE

ScalaTest, SBT, Debugging and my IDE

I found this a surprisingly difficult configuration to find out about, but once working, it is pure awesome; Java debugging, hide in shame.

Once you have a basic SBT build configuration configured (more on that in another post I think), you can run Spec tests easily enough by using the 'test' command in SBT. What seemed rather bewilderingly difficult to find with a Google search was how to run only a single test file.

 You can run a single ScalaTest Spec test with the "test-only" command followed by the full class name:

 > test-only com.plexq.darkhorse.conquest.ConquestMultiTagSimpleSpec

It's that easy, kinda embarrassing it took so long to figure out (finally found it in the SBT FAQ I think)!  So, now we have that piece down, we come to debugging.  This was so blindingly obvious, it just didn't occur to me.  Just add the normal JVM options for debugging, and given that this is a Scala blog, I'm going to assume you're running Java 1.6 or better:

Here's what I use:

-Xmx512M -XX:MaxPermSize=256M -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

This dovetails nicely with the default debug port, at least in IntelliJ.  In IntelliJ 11, you have the ability to run an SBT console (you have to install the plugin though). So, I added these options on the console configuration dialog which can be reached through: IntelliJ -> Preferences -> (Project Settings) SBT : VM parameters.

Kick the SBT console into life (which can be found as a tab/button on the bottom bar), and run the test once. That's right, run the test once before attaching the debugger.  I found if I try to attach the debugger before having executed something in SBT, it gets pretty grumpy and often doesn't work.

Now for the magic that makes SBT so awesome, combined with real-time debugging that makes IDEs so awesome; run your test case in monitoring mode (forget the real name):

> ~ test-only com.plexq.darkhorse.conquest.ConquestMultiTagSimpleSpec

Every time you change source files after a debug run, SBT will re-run the test for  you with the debugger attached.  After a debug run, and a round of changes, don't terminate the debugger if you can, just click through continue until there's no more to do.  Save the file you changed, and SBT will kick immediately into a new debug run, which will throw the debugger console up as soon as it hits a break-point.

Immediate and fast debug cycles ensue, allowing problem resolution in record time.

On the debugger attachment, I found that if just hit stop on the debugger, which allows the program to continue without the debugger running, and then attempt to re-attach, it causes problems.  Sometimes the next task in SBT will hang, sometimes it works.  Not sure what the deal with this is, but if they can fix this, so much the better, it would remove the need to pound the continue button until it's done.

Overall though, I find this pretty awesome.  Stops me having to alt-tab around which prevents my ADD brain from getting distracted by something else that interrupts whilst I'm waiting for a compile or when I  task-switch, see I have a new IM or email, and just kinda get lost in that.

2 comments:

  1. Nice article, but background picture is terrible - text is not readable.

    ReplyDelete
  2. hi, can you write up step by step tutorial for debugging sbt test command with eclipse?
    it would be a great help
    thanx :)

    ReplyDelete