Friday, July 10, 2009

metric_fu, rcov, and rspec

I've been setting up metric_fu on one of my projects, and I ran into a problem where the rcov report was not including my specs. (This is an old project that has both Test::Unit tests and RSpec specs.) I did lots and lots of digging, and was baffled that no one has mentioned that they cannot run coverage reports for their specs using metric_fu. It seemed like an obvious and heinous oversight on the part of metric_fu.

As always, I suspected that I was missing something. Test::Unit tests can be run individually from the command line like ruby path/to/test, and metric_fu appeared to be trying to do this with the specs, but when I did ruby path/to/spec I got nothing.

I actually looked at the metric_fu specs to discover this, and it is probably very obvious to any serious RSpec user, but I discovered that I needed to add require "spec/autorun" to the top of my spec_helper.rb file. This includes the magic that makes ruby path/to/spec work, and makes rcov include my specs in its report when it is invoked by metric_fu.

3 comments:

Jake Scruggs said...

It's not as obvious as you might think -- I made the same mistake. Back in the early days of of RSpec, all you had to require was 'spec' but now you need to require 'spec/autorun'. I kept wondering why when I ran metric_fu on itself the coverage was terrible (If you just require 'spec' the specs won't run automatically when you pass them to rcov) until I figured that out.

Unknown said...

Thank you for this post. It just saved my day.

Anonymous said...

You saved my life..