CoverMe – Code Coverage for Ruby 1.9
Ruby 1.9(.2) is an amazing language to develop applications in. It’s faster, more powerful, cleaner, and a huge improvement over Ruby 1.8.×. Because of those reasons every Ruby developer should move to this exciting new version of our language.
When making a move of this size it’s important to have the right tools to help us along. Unfortunately, one of the most useful tools as a Ruby developer, RCov, does not work with Ruby 1.9.
RCov, for those unfamiliar analyzes your code and tells you which part of your code was not executed. This is INCREDIBLY useful when hooked up to your test suite. While, it’s not the only metric you should use when determining how good your test coverage it, it certainly is a great first step to point out exactly which parts of your code haven’t been touched at all!
Enter CoverMe.
History
While working on a Ruby 1.9/Rails 3 project, and loving everything about it (except for the lack of RCov), I came across a post by Aaron Patterson (of Nokogiri fame). In this post he quickly outlined a very basic coverage tool using the new built-in Coverage module in Ruby 1.9.
After spending a morning playing with it, I was quickly able to grow the idea into something useful for the project. Later that day the company I was consulting for (BiddingForGood.com), and in particular their chief architect, Stuart Garner, told me to take a day or two and clean it up and release it for the world to use, and so here it is.
Features
Index Page
- Sortable column headers (File, Lines, Lines of Code, Tested %).
- Searching/filtering by file name.
- Filtering by coverage percent.
- Color coded list of files to quickly see which ones are 100% covered, > 90% covered, or less than 90% covered.
- Large color coded average coverage percent, for quick reference.
Detail Page
- Line by line coverage report
- Color coded lines to quickly see which lines where executed and which ones were not.
- Side by side viewing with the corresponding test/spec file (if one exists).
Use
CoverMe is pretty easy to use, and has been tested with Ruby 1.9.2 and Rails 3. First, add CoverMe to your Gemfile:
gem 'cover_me', '>= 1.2.0', :group => :test
Now make sure to install the gem with Bundler:
$ bundle install
All that’s left now is to require it as the first line of your ‘spec_helper.rb’ or ‘test_helper.rb’ file:
require 'cover_me'
Now the next time your run your test suite it will create a series of HTML files in the coverage folder at the root of your project. Open coverage/index.html and you’re off and running.
Rails 3
If you are using CoverMe with Rails 3 run the following to get the cover me Rake task:
$ rails g cover_me:install
Configuration
CoverMe is easily configurable, under the covers it uses the Configatron library. Here are a few quick config options you might want to play with:
CoverMe.config do |c|
# where is your project's root:
c.project.root # => "Rails.root" (default)
# what files are you interested in coverage for:
c.file_pattern # => [
/(#{CoverMe.config.project.root}\/app\/.+\.rb)/i,
/(#{CoverMe.config.project.root}\/lib\/.+\.rb)/i
] (default)
# what files do you want to explicitly exclude from coverage
c.exclude_file_patterns # => [] (default)
# where do you want the HTML generated:
c.html_formatter.output_path # => File.join(CoverMe.config.project.root, 'coverage') (default)
# what do you want to happen when it finishes:
c.at_exit # => Proc.new {
if CoverMe.config.formatter == CoverMe::HtmlFormatter
index = File.join(CoverMe.config.html_formatter.output_path, 'index.html')
if File.exists?(index)
`open #{index}`
end
end
} (default)
end
Contributors
- Mark Bates
- Andrea Campi
- KITAITI Makoto
- Alex Crichton
- Leif Bladt
- Will Marshall
- Matthew Albright
- Josef Sin
- Lasse Koskela
- Stephen Delano
- George Anderson
- Rob Zolkos
- jmthomas