I’ve started to use rcov on all my Rails projects. A project I had started working on last year had already been doing it but it didn’t really click in my brain until recently. Rcov is a great code coverage tool for Ruby and Mike Clark has a great write-up on using rcov with Rails plus a sample rake task. It includes a command to open up the generated HTML automatically which I usually do afterwards.

I only have one small tweak to Mike’s rake task and that is to sort by coverage by adding “–sort coverage”. What this does, at least for me, is to let me know what classes really need some testing love. And isn’t that what your Ruby classes need this Valentine’s Day? :)

Here’s the patched rake task (credit again goes to Mike Clark for the original rake task):

namespace :test do

  desc 'Measures test coverage'
  task :coverage do
    rm_f "coverage"
    rm_f "coverage.data"
    rcov = "rcov --sort coverage --rails --aggregate coverage.data --text-summary -Ilib"
    system("#{rcov} --no-html test/unit/*_test.rb")
    system("#{rcov} --no-html test/functional/*_test.rb")
    system("#{rcov} --html test/integration/*_test.rb")
    system("open coverage/index.html") if PLATFORM['darwin']


3 Replies to “Rcov”

  1. hey man, i searched rcov on google and you were number 5, really cool :-)

Comments are closed.