Wednesday, 17 July 2013

Setting up Grails-guard

I'm currently working on a project using Grails. Earlier this week I found myself losing my concentration whenever I fired up the integration tests simply because it took too much time to execute them. By the time they were finished my mind had drifted off somewhere else. Clearly I had an itch!

On my last project where Rails was used, Spork and Guard was utilized to keep the Rails environment running and re-executing the tests. That allowed me to enjoy fast running tests whenever code had changed. Was there any alternative to this in Grails? Someone recommended Infinitest, but that seemed only to cover the unit tests. I did however find an alternative called Grails-Guard that runs the integration test whenever there are changes to the code. This way the roundtrip time is a fraction of what I started with. For the record, I've only set this up using the command line, but it should be fairly straight forward using IntelliJ or some other IDE as well. Oh, and by the way, Grails-Guard only supports Grails 2.0.0 and above.

Firstly you need to include Grails-Guard in your project. The latest version when this post was written was 1.0.7. We use BuildConfig.groovy to handle dependencies in the project, so I simply added this line to the file inside the plugin block.

Then you need to ensure that the test-app have an active spring reloading agent, and secondly that the testing is run using guard mode instead of integration mode. This can be done using this command:

This command will scan the integration test directory and re-run all the integration tests when changes occur in the code. Let's create an alias for this so we don’t have to write this line every time. Go to the root of your home directory and add this to your .bashrc, .zshrc or whichever shell you prefer:

If you want to run a single file, or only files in a given package structure this can easily be done by specifying the file names separated by spaces, or the package structure. Open up a new terminal window and type something like this:

The grails environment should start, run the tests and wait for changes. Whenever you save a groovy file within your application that should results in a recompile and the test should re-execute automatically. The reason I'm using g-guard as my alias instead of just guard is that I already have guard (the ruby gem) assigned to that alias.

My itch is gone!

No comments:

Post a Comment