I have been using SimpleCov to measure the code coverage of my specifications, and it has been excellent. I would recommend it, if only for how easy it was to get it working with RSpec.

I have run into one problem though – running it under Spork generates no results! As far as I can tell, this is a problem with the way Spork caches your application files. SimpleCov uses the Ruby 1.9 built in profiling features, and either 1.9 or Spork need the files under inspection to be loaded after the profiling has started. Unfortunately your application files aren’t reloaded when you are using Spork, that’s kind of the point!

Some people seem to be able to get away with merely turning off application file caching in config/environments/test.rb, but that didn’t work for me. Instead, I have come up with a different solution: Only turn on code coverage reporting when I run RSpec outside of Spork. At the top of spec/spec_helper.rb I have the following block:

1
2
3
4
5
6
7
if(ENV["RUN_COVERAGE"])
  require 'simplecov'
  SimpleCov.start 'rails' do
    add_filter "/spec/"
  end
  puts "Running coverage tool\n"
end

That way I can run RUN_COVERAGE=true rspec spec/ in the root of my application directory, and RSpec will run with SimpleCov enabled. As an added bonus, the code coverage metrics aren’t running on every test run, so my tests are faster!

  2 Responses to “Getting SimpleCov and Spork to play together”

  1. Thanks, I was having the same problem, and this worked for me as well. Though I also found I needed to stop spork running before running the tests to get the coverage to run; typically when I’m running the full suite to get coverage, I don’t need spork running anyway. When spork is running, I’m testing one or a few files at a time, in which case the extra speed from not running coverage reports helps too.

  2. Weird, I don’t need to stop spork. I’m not running the second copy of rspec with the --drb option so I figure it isn’t running under spork. Anyway, glad I could help!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

 
© 2011 Jamie's Space Suffusion theme by Sayontan Sinha