I wanted to use SimpleCov within one of my latest Rails applications, but I wanted it to be a seperate rake task instead of running all the time.

First add SimpleCov to your Gemfile.

gem 'simplecov', '~> 0.4.2'

Add a new task to your Rakefile, this is an example with RSpec.

desc "Run tests with SimpleCov"
Spec::Rake::SpecTask.new('coverage') do |t|
  ENV['COVERAGE'] = "true"
end

Then add the following to the top of your spec_helper.rb file.

if ENV['COVERAGE']
  require 'simplecov'
  SimpleCov.start
  # For rails applications use
  # SimpleCov.start 'rails'
end

Published on Monday, 25 July 2011