Wednesday, April 23, 2008

code-coverage in the rails environment

I picked up my todo list for the startup (name to be revealed) tonight, and didn't really have anything that could be done in less than the two-hour time window I had available, so I decided to install RCov (code coverage gem for ruby on rails) and see how I was doing on my test coverage. Now I'll be the first one to say that a good score on a coverage tool does not mean that your tests are testing the RIGHT THINGS, but a bad score does indicate that some things aren't being tested AT ALL. So I've decided to post the results today to publicly show just how well I'm doing, and maybe I'll do that again every month or so to keep myself motivated to keep the coverage level as close to 100% as possible.

It turns out this RCOV is pretty easy to use. I just installed the gem:

gem install rcov

then I created some rake tasks to actually run the coverage scenarios:

namespace :test do

task :prepare_rcov do
rm_f "coverage"
rm_f ""
Rcov = "rcov --rails --aggregate --text-summary -Ilib"

desc 'Measures unit test coverage'
task :unit_coverage => :prepare_rcov do
system("#{Rcov} --html test/unit/*_test.rb")

desc 'Measures functional test coverage'
task :functional_coverage => :prepare_rcov do
system("#{Rcov} --html test/functional/*_test.rb")

desc 'Measures integration test coverage'
task :integration_coverage => :prepare_rcov do
system("#{Rcov} --html test/integration/*_test.rb")

then I fired up the command line, and here are my results:


76.3% 14 file(s) 440 Lines 358 LOC


85.6% 17 file(s) 879 Lines 736 LOC


I have no integration tests written

So, that's a pretty embarrassing level to start at. Luckily there's nowhere to go but up. If you have a rails project you're working on, I recommend giving yourself a reality check on just how well you're testing your code: it only took 15 minutes for me to realize I've got a lot of work ahead of me. ; )

1 comment:

mike said...

have you complete sample code in place ?
i'm looking to add coverage with checking to an integration build i have going.

i want coverage to be run after the tests, and to fail if it's not over a certain level