Sunday, June 24, 2007

CruiseControl.rb and RCov Are SO Good Together

I spent a bit of time this morning getting CruiseControl.rb and RCov working together for a new client's fairly LARGE Ruby on Rails (70+ models, 40+ controllers) project. If you are not familiar with CruiseControl.rb, it is the Ruby based continuous integration tool from ThoughtWorks. RCov, on the other hand, is a very well known code coverage tool for Ruby. The implementation was incredibly simple, but finding the information on how to get the two working together required a perusal of the CruiseControl.rb code itself.

First of all, you will need to install the rails_rcov plugin. This useful plugin allows you to really easily run RCov using rake, and is essential to simple RCov integration with your CruiseControl.rb builds. You can install rails_rcov like this:

./script/plugin install

Use the "-x" option if you want to use an svn:external link to the latest version of the plugin. I personally don't like to do that...I want control over when the software I am using gets updated, thanks.

Once the plugin is installed into your project you have some useful rake tasks like this one:

# sort by coverage
rake test:units:rcov RCOV_PARAMS="--sort=coverage"

Now you are ready for the final bit, which is adding a "cruise.rake" task to the "lib/tasks" directory of your Ruby on Rails project. The task should have something like this, which was lifted verbatim from the CruiseControl.rb build of CruiseControl.rb itself (that all sounds kinda twisted, but it is cool):

desc 'Continuous build target'
task :cruise do
mkdir_p out unless out if out

ENV['SHOW_ONLY'] = 'models,lib,helpers'
mv 'coverage/units', "#{out}/unit test coverage" if out

ENV['SHOW_ONLY'] = 'controllers'
mv 'coverage/functionals', "#{out}/functional test coverage" if out


That's it. Now you have incredible code coverage goodness right within your build. You can even browse the source within the browser window of the CruiseControl.rb dashboard and see which lines of code were covered by your tests, and which were not...

So if you have a Ruby on Rails project, with a project team of greater than 1 developer, you had better start using CruiseControl.rb. It is too easy, and the payoff of doing continuous integration is huge!


Shawn said...

Hi Ron, this is Shawn from CW. I just wanted to say hello, nice blog, and to wish you success in your future endeavors. I can be reached at shawn at shawnwhite dot biz.

Trish said...

Thanks so much for posting this! It was a great help.

Jim Remsik Jr said...

Thanks Ron,

You turned this into a 3 minute task for me, including adapting for rspec.

MRoderick said...

Yup, works just fine.

David Medinets said...

It's 2009 and I still found your blog entry useful,

Sanjeev Kumar Mishra said...

This is really amazing to see... I integrated the rcov today to a new project and it just works fine without any hassle the way u have described here ...

thx a lot man

cheers :D

Sanjeev Kumar Mishra said...

amazing stuff.. works really smooth

thx a lot :D