Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Still Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

TumblrIf you're ADD

RCov for almost any occasion

I hope I don’t have to tell you why it’s awesome to be testing. I’ll take that as a given.

Once you’re testing, it is useful to be able to see much of your code is being touched by your tests. This is where rcov makes an entrance. It does some sorcery in order to do just that: tell you what lines of your code your tests ran.

I’m not going to talk about the pros/cons of using code coverage, just going to tell you how to use rcov in most Ruby projects you might run into. For further reading, here’s some linkly love to get you started:

I’m mainly putting this together because I always had trouble finding it myself, and figured it’d be useful to have for future reference.

Get ur rcov right here

The main rcov website can be found here. I have seen some annoying segfault problems when running that version, so have taken to using this variation on GitHub. Install it with:

# Run the following if you haven't already:
gem sources -a
# Install the gem:
sudo gem install spicycode-rcov

Using it

I’ve wanted to use rcov in three different settings. I’m sure there’s more, but it’s what I’ve dealt with so far.

  • A RubyGem project using Test::Unit
  • A RubyGem project using RSpec
  • A Rails project

RubyGem using Rake and Test::Unit

You just need to update your Rakefile to include something like:

require 'rcov/rcovtask' do |t|
  t.libs << "test"
  t.test_files = FileList['test/*_test.rb']
  t.verbose = true

Now you can run rake rcov and then see the report at coverage/index.html.

RubyGem using Rake and RSpec

Similarly, you just need to update your Rakefile to do:

require 'rspec/spec_task'"rcov_spec") do |t|
  t.spec_files = FileList['spec/**/*_spec.rb']
  t.spec_opts = ['--color']
  t.rcov = true
  t.rcov_opts = ['--exclude', '^spec,/gems/']

Now you can run rake rcov_spec and then see the report at coverage/index.html.

Rails Project

There’s several ways out there of including coverage in your Rails app. I found the most direct was just to use the metric_fu plugin.

script/plugin install git://

After it’s installed, among other things, you can run rake metrics:coverage and see the report at metrics/coverage/index.html.


Had a few things pointed out:

  • Rcov::RcovTask is in rcov/rcovtask, not rcov/rcov_task. Caught by hardbap.
  • The origin glob for that task only caught files in test/*_test.rb. test/**/*_test.rb would catch subdirectories too. Caught by fowlduck.
  • fowlduck found some problems when using metric_fu as a gem, and posted work arounds in the comments.
comments powered by Disqus