rcov Rake Task

January 05, 2007

Jamis recently posted a tip on the virtues of running rcov to measure code coverage. While it's easy to put too much faith in code coverage, running rcov is an inexpensive way to get some insight into your testing patterns. I've been using it on recent Rails projects to get a quick synoptic view of the test landscape.

If you'd like to give rcov a try, here are the steps I used to get it working:

1. Install rcov

gem install rcov

2. Write a Rake Task

Add this task to your lib/tasks/my_app.rake file, for example:

namespace :test do

  desc 'Measures test coverage'
  task :coverage do
    rm_f "coverage"
    rm_f "coverage.data"
    rcov = "rcov --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']
  end

end

I've tried various approaches and options over the last few months, and these seem to work best for me. The results for all the unit, functional, and integration tests get aggregated into one report so I can scan everything quickly. Your mileage may vary.

3. Run the Task

rake test:coverage

You'll get a text summary for the unit, functional, and integration tests as they're being run. And if you're on a Mac, the task automatically pops open the HTML report in your default browser at the end. Otherwise, you can open it manually.

Enjoy!

Read more posts in the blog archive »