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

Miscellaneous Tips and Tricks


Tips and Tricks

This article is part of a series. For more information, see Code Coverage with SimpleCov.

The following are smaller tidbits I found useful, but didn't warrant a complete article by themselves, or at not interesting to everyone using SimpleCov.

Load SimpleCov, or Don't, I Don't Care

Maybe you're apathetic about code coverage. Maybe you use it but some developers on the team don't. Be indifferent about whether Ruby actually loads the SimpleCov library by wrapping the require statement in an exception handler. This takes advantage of the fact that require is just a method, if it fails it raises an exception like any other method (in some language, things like "require" or "include" are special, and are treated differently).

  require 'simplecov'
rescue LoadError => e
  puts "Could not load SimpleCov, continuing without code coverage"

This can be extended to any optional gem, really. If you want the functionality, install the gem. You could also use a command-line option or environment variable to implement similar functionality.

Surgical Strike

If you're working on something specific and want to filter out all files you're not interested in at the moment, you can easily implement this by using add_filter with a block and an environment variable. This way, you can concentrate on just the coverage you want at the moment.

You can either put this block around just your filter, or you can have completely separate SimpleCov.start blocks. Here, I just did the whole block to keep things a bit cleaner. Also, notice that the logic is backwards here. We're defining the files we want to see, while filters usually define files you don't want to see, so don't miss that not int he add_filter call.

Spork.prefork do
  unless ENV["DRB"]

    if ENV["COV_FILES"]
      require 'simplecov'
      SimpleCov.start do
        add_filter do|file|
          not file.filename.include?(ENV["COV_FILES"])
      require 'simplecov'
      SimpleCov.start do
        add_group 'Models', '/app/models/'
        add_group 'Controllers', '/app/controllers/'
        add_group 'Views', '/app/views/'
        add_group 'Helpers', '/app/helpers/'

        add_filter '/config/'
        add_filter '/spec/'

        project_name 'Rails Tutorial Sample App'
        coverage_dir 'cov'

Reusable Filters

If you have multiple SimpleCov.start blocks as in the previous example, it may be useful to re-use a filter. If your filter is a block, there's no easy way to do that. You could define a method that returns a block or something like this, but SimpleCov thought of this. You can use a "filter class" that also allows you to parameterize your filters. How handy! You can use any class really, as long as it responds to the matches? method, which takes one argument (a SimpleCov::SourceFile object). It should return true if it wants to filter that file, otherwise false.

So we can implement a filter that will open the file and search for the string # DO_NOT_COVER, that way we can define which files we don't want coverage on in the files themselves. So, if you have a model that's covered by another test suite, for example, in the model file you can put the comment above, as well as in any other applicable files, and the filter will just ignore it.

class FileContains < SimpleCov::Filter
  def initialize(str)
    @str = str

  def matches?(file)
    file.src.find {|line| line.include?(@str) }

Spork.prefork do
  unless ENV["DRB"]

    SimpleCov.start do
      add_filter FileContains.new("# DO_NOT_COVER")

Integrate with Vim

Yeah, SimpleCov can output an HTML file that lets you browse through the source code and see what's covered, but then you still have to switch back to your editor (the one true editor is Vim, or... erm, that's the only editor covered here!). However, there is a gem that outputs a Vim script that will automatically highlight your source code with your code coverage. How cool is that?

To use this, just:

  1. Add gem 'simplecov-vim', :require => false to your gem file.
  2. Run bundle install.
  3. Make sure to require simplecov-vim/formatter.
  4. Add a formatter SimpleCov::Formatter::VimFormatter to your SimpleCov configuration.
  5. And run your tests.

As part of the output, you'll get a coverage.vim file in your project root directory (might want to pop that into your .gitignore) and, when editing any file in your project using vim, you can now just enter the command :so coverage.vim to get arrows on the left showing which lines have and have not been covered. Simple, but very useful!

Central Configuration

Have several projects that all use the sample SimpleCov configurations? You can be DRY without trying to hack this functionality in yourself, you can place a .simplecov in the root of your project. If this file exists when you require simplecov, it will also automatically load this file. In this file, you should put your entire SimpleCov.start block, as well as any related code such as filter classes. This is especially useful if you have more than one test suite, or even tests for sub-projects.

  1. About.com
  2. Technology
  3. Ruby
  4. Gems
  5. SimpleCov
  6. Tips and Tricks

©2014 About.com. All rights reserved.