Grails TDD simplified
Let me start by saying that Testing Grails sucks. This is not to say that there aren’t supporting tools and technologies. There is super classes GrailsUnitTestCase, GroovyTestCase etc. and there is few solutions for mocking needs. However, in general, the cycle of writing a failing test, execute to fail, some code to make is pass, execute to verify passed and refactor takes a long time. This “long time” phrase is relative and I mostly compare with folks coming from plain-old Java world or from Ruby world. I have experience with both and grails is slow relative to both.
Of course there is some progress being made so that you are able to run specific test class using following command:
# specify <Class> without the trailing "Tests" $ grails test-app [-unit | -integration ] <Class>
The above command makes is so that you don’t have to run entire test suite. This still leaves a few things desirable:
- The user still have responsibility to specify -unit or -integration depending on which test is being run. The framework doesn’t figure this out automatically.
- The above command won’t fail is the specified test class is not found.
- When the test fails, the stack trace is long, long, very long. It takes pathetically long time to find the line that points to the problem in your test.
So, I wrote following script (in Ruby!) to resolve above issues.
Script: gtest
#!/usr/bin/ruby if ARGV.length != 1 puts "Expected 1 argument." exit -1 end test_name=ARGV[0] unit_or_int="" test_file_found=false if File.exists?("test/unit/#{test_name}Tests.groovy") test_file_found=true unit_or_int="-unit" elsif File.exists?("test/integration/#{test_name}Tests.groovy") test_file_found=true unit_or_int="-integration" else puts "No Unit or Integration test found for #{test_name}" exit -1 end testfile="test/reports/plain/TEST-#{test_name}Tests.txt" #system("touch #{testfile}") cmd="touch #{testfile}; (grails test-app #{unit_or_int} #{test_name} &); tail -f #{testfile} | filter_groovy_trace" puts cmd exec(cmd)
Script: filter_groovy_trace
grep -Ev "at org.codehaus.|at groovy.lang|at gant.Gant|at TestApp_groovy|--Output from test|--------|at org.gmock.internal|^Testcase: |at sun.reflect| at net.sf.cglib|at java.lang.reflect"
With these two scripts in my path all I need to do is:
gtest <Class>
I have been using it for over a month and it has served me well. It still doesn’t come close to what you could do in Java (execute individual test from inside IDE) or ruby (the java runtime initialization takes for ever compared to ruby runtime), but it reduces the pain considerably.
There is one more thing we can do to make it faster. Take out the xsl transformation at the end of test execution. I couldn’t find the command line option for that. I know it exists.
Reduced Pain => Enjoy!
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.





