Using Rcov to measure the test coverage of Rails plugins

To view the coverage of your plugins using Rcov, first install the rcov gem with sudo gem install rcov, then copy and paste the following onto the end of the Rakefile inside your plugin directory:


require 'rcov/rcovtask'

desc 'Measures test coverage using rcov'
namespace :rcov do
  desc 'Output unit test coverage of plugin.'
  Rcov::RcovTask.new(:unit) do |rcov|
    rcov.pattern    = 'test/unit/**/*_test.rb'
    rcov.output_dir = 'rcov'
    rcov.verbose    = true
  end
  
  desc 'Output functional test coverage of plugin.'
  Rcov::RcovTask.new(:functional) do |rcov|
    rcov.pattern    = 'test/functional/**/*_test.rb'
    rcov.output_dir = 'rcov'
    rcov.verbose    = true
  end
end

You can now simply run rake rcov from inside your plugin directory which will generate an rcov directory with the results. Open rcov/index.html (if you are on OSX this will open automatically) in a browser to view the results.

Thanks to “Mike Clark”:http://clarkware.com/cgi/blosxom for his “Rcov rake task for Rails”:http://clarkware.com/cgi/blosxom/2007/01/05#RcovRakeTask which this task is based on.

*Update (11-11-08):* Changed code to use the RcovTask class.

Published by

Jamie

Hi, I am the Managing Director of SonicIQ Limited in the UK. I have been working in the web development industry since 1999 and have been running SonicIQ since 2001. Currently Ruby On Rails is my preferred development platform. I am experienced in designing with web standards, HTML5, CSS3 and Javascript.

4 thoughts on “Using Rcov to measure the test coverage of Rails plugins”

  1. Thanks for the write up. I gave this a shot but I am seeing rcov test all of my /Library-Ruby-Gems-1-8/ gems. Is there a feature to turn off the checking of those gems? I just want to run the rcoverage testing on my app/test/* files.

    Thanks!

  2. Problem solved… Turns out I needed to exclude the gem directories. I had to modify your code a bit so without pasting up a very distorted version I have included my logic below, should anyone come to face this issue:

    ———————– Logic to set exclude variable

    if PLATFORM =~ /darwin/
    exclude = ‘–exclude “gems/*”‘
    else
    exclude = ‘–exclude “rubygems/*”‘
    end

    ———————– Example implementation

    require ‘rcov/rcovtask’

    desc ‘Measures test coverage using rcov’
    namespace :rcov do
    desc ‘Output unit test coverage of plugin.’
    Rcov::RcovTask.new(:unit) do |rcov|
    rcov.pattern = ‘test/unit/**/*_test.rb’
    rcov.output_dir = ‘rcov’
    rcov.verbose = true
    rcov.rcov_opts << ‘–exclude “gems/*”‘
    end

    desc ‘Output functional test coverage of plugin.’
    Rcov::RcovTask.new(:functional) do |rcov|
    rcov.pattern = ‘test/functional/**/*_test.rb’
    rcov.output_dir = ‘rcov’
    rcov.verbose = true
    rcov.rcov_opts << ‘–exclude “gems/*”‘
    end
    end

    —————————
    Odd why this has happened to me and not others… Unless I just missed the step most people don’t — to exclude the gems folders. Thanks for your help in getting me started!

  3. FWIW, I also had that happen to me. Your –exclude parameter option worked like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>