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 for his Rcov rake task for Rails which this task is based on.
Update (11-11-08): Changed code to use the RcovTask class.
3 comments ↓
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!
I’m not sure what’s happening there, I have updated the post to reflect the code that I use now.
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!
Leave a Comment