Add RCov to your build in two minutes
We are big believers in the value of code coverage. Code coverage can be defined in different ways depending on language and the tool being used. With Ruby the tool is rcov and the type of coverage is “statement coverage”, also known as line coverage. Statement coverage basically shows what code was executed while your test suite ran. Although this is a coarse measurement, it can still be a valuable first line of defense. For a much more in depth discussion of the ins and outs of coverage, start with Jason Rudolph’s Brief Discussion of Coverage Types, and continue on with his superb Testing Anti-Patterns: How to Fail with 100% Coverage series.
Integrating coverage reports into your day-to-day build process is very easy. I’m going to show how to do it with plain text coverage reports, which is the simplest way, but it’s also easy to output them to HTML. For a real world project using plain text coverage, you can take a look at our open source Ruby CAS server called Castronaut.
First, the Rakefile requires the built-in RSpec verify_rcov task:
require 'spec/rake/verify_rcov'
Then, the verify_coverage task is created with a coverage threshold of 99.2% - if the code coverage falls below this level, the build will fail.
RCov::VerifyTask.new(:verify_coverage => :specs_with_rcov) do |t|
t.threshold = 99.2
t.index_html = 'coverage/index.html'
end
The verify_coverage task has a dependency on the specs_with_rcov task, which is a basic RSpec task defined to run with rcov turned on, and with the coverage options set to display the report to stdout.
desc "Run all examples with RCov"
Spec::Rake::SpecTask.new('specs_with_rcov') do |t|
ENV["test"] = "true"
t.spec_files = FileList['spec/**/*.rb']
t.rcov = true
t.rcov_opts = ['--text-report', '--exclude', "spec,Library,lib/castronaut/db,#{ENV['GEM_HOME']}", '--sort', 'coverage']
t.spec_opts = ['-cfn']
end
Finally, the default task is defined to run the verify_coverage task, which will in turn run specs with rcov:
task :default => [:verify_coverage]
As an extra bonus of setting up coverage, you’ll get the coverage output and coverage verification in your RunCodeRun build. For example, the screen shot below shows the output on RunCodeRun with the relevant coverage and verification passing:

Easy!