Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Using RCov with Cucumber and Rails

mattwynne edited this page Aug 13, 2010 · 18 revisions

How to use RCov with Cucumber and Rails

This page serves as an easy way to learn how to use cucumber and RCov together.

Quick example

First, Aslak recommends that you use spicycode’s RCov instead of the ‘official’ one, as it currently segfaults too much for most people’s taste.

gem sources -a http://gems.github.com
gem uninstall rcov
gem install spicycode-rcov

Second, you’ll need to open up the cucumber generated task file and set the rcov config option.

#lib/tasks/cucumber.rake
$:.unshift(RAILS_ROOT + '/vendor/plugins/cucumber/lib')
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |t|
  t.cucumber_opts = "--format pretty"
  t.rcov = true
end
task :features => 'db:test:prepare'

Now when you run rake:features, coverage information will be in rails_root/coverage

Other ways to run features

See Running features

Options

You usually want to define a task to run all your features without RCov and then have an additional task to run with RCov. Also, you usually need to specify certain options to RCov, such as output directory, for cruise control and other build purposes. Below is an example cucumer.rake file showing how to set options and define multiple cucumber tasks:

desc "Run all features"
task :features => "features:all"
task :features => 'db:test:prepare'
require 'cucumber/rake/task' #I have to add this -mischa

namespace :features do
  Cucumber::Rake::Task.new(:all) do |t|
    t.cucumber_opts = "--format pretty"
  end

  Cucumber::Rake::Task.new(:cruise) do |t|
    t.cucumber_opts = "--format pretty --out=#{ENV['CC_BUILD_ARTIFACTS']}/features.txt --format html --out=#{ENV['CC_BUILD_ARTIFACTS']}/features.html"
    t.rcov = true
    t.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/}
    t.rcov_opts << %[-o "#{ENV['CC_BUILD_ARTIFACTS']}/features_rcov"]
  end

  Cucumber::Rake::Task.new(:rcov) do |t|    
    t.rcov = true
    t.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/}
    t.rcov_opts << %[-o "features_rcov"]
  end
end

(This was taken from bmabey’s cc.rb setup)

It is important to note that Cucumber’s default RCov options are set to %w{—rails —exclude osx\/objc,gems\/}. So you can just append to rcov_opts if those defaults work for your project.

Focused Coverage

When retro-fitting features to legacy code, it’s good to be able to focus the coverage from your scenarios on just one class at a time. You can so this by calling rcov and cucumber directly from the command line:

rcov -t -x \.rb -i app/controllers/my_legacy_controller.rb ./vendor/plugins/cucumber/bin/cucumber -- features/visitor/see_my_shiny_page.feature features/user/see_my_shiny_page.feature 

What do these options mean?

  • -t tells rcov to print out a little text summary
  • -x \.rb tells rcov to ignore all ruby files, except…
  • -i tells rcov to watch coverage of the one file you want to focus on
  • -- tells rcov to send the final arguments to cucumber, so these are your normal cucumber args, in this case listing the features we want to run to exercise the class we’re testing.

As above, you’ll have a full coverage report in ./coverage. Open it using open coverage/index.html

Clone this wiki locally