1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Simple code coverage for Ruby


Code coverage metrics are an important, but often ignored, part of test-driven development. Code coverage tools like SimpleCov give you an overview of how many lines of code your tests cover, and which lines are and are not covered. Code coverage is not a perfect tool though, SimpleCov won't tell you how good your tests are or what they actually tested for, but they'll at least tell you there is code that isn't covered by any tests, which is the very minimum a good test suite should do.

At its heart, SimpleCov is a pretty simple piece of software, and not difficult to use either. You more or less load it up before your tests, fire and forget and you get an HTML report as output. There is a bit of work to be done, especially in large projects, to categorize your files and filter out the unneeded files. The following articles walk you through many aspects of using SimpleCov effectively.

  • Simple Code Metrics will walk you through installing SimpleCov and the most basic usage. It'll also look at a simple, single file example to get your feet wet and let you see what happens when you have uncovered lines.

  • Filtering and Grouping covers the bulk of the work to be done when working with SimpleCov. We load up an example Rails project and talk about how you could filter out irrelevant files (such as the tests themselves) and group the relevant files into groups that appear as tabs at the top of the HTML output. And while there is a Rails adapter for SimpleCov that does all this for you, it's a good way to get your hands dirty and actually configure SimpleCov.

  • Tips and Tricks goes through a few things I found useful that don't really warrant an entire article in themselves. Here you'll learn how to, when needed, get coverage for only a single file, write filters that you can re-use in different invocations of SimpleCov (so you can include a filter class along with a library, for example) and finally integrate SimpleCov output into Vim. This last tip is especially useful for Vim users, as with a single command you can see which lines are and are not covered in your tests, right there in your editor.

Technically, Ruby has code coverage built in already. However, it's a bit primitive and it's more of a low-level API than something you can just use. But, if that's what you're after, you can get more information about it from the official documentation. Or you can just start playing with it. The basic gist of it is to require coverage.so (a ".so" file means it's a compiled extension, written in C), call Coverage.start, run your code and then your coverage data is in Coverage.result. So let's fire up IRB and try this out.

$ irb
2.0.0p247 :001 > require 'coverage.so'
 => true
2.0.0p247 :002 > Coverage.start
 => nil
2.0.0p247 :003 > require './dice.rb'
 => true
2.0.0p247 :004 > Die.roll "3d6"
 => 3
2.0.0p247 :005 > Coverage.result
 => {"/Users/user/work/simplecov/dice.rb"=>[nil, nil, nil, 1, 1, nil, 1, 1, nil, nil, 1, 3, nil, nil, 1, nil, nil, 1, 1, 1, 0, nil, nil, 1, 1, 1, nil, 1, 3, nil, nil, nil, nil, nil]}

And this is the interface you get. You get a hash whose keys are filenames and values are a list of nils and numbers, representing the number of times any particular line in the file was executed. This can be rolled into something more without much work, but as it stands, you really need something like SimpleCov to get much use out of something like this. But if SimpleCov doesn't meet your needs, this at least gives you something to build on.

  1. About.com
  2. Technology
  3. Ruby
  4. Gems
  5. SimpleCov
  6. SimpleCov - Simple Code Coverage in Ruby

©2014 About.com. All rights reserved.