Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

Code coverage with SimpleCov in under 2 minutes

10 June 2014

I was seriously impressed today with SimpleCov, a code coverage test tool for Ruby. I went from wanting to have coverage to actually having the numbers in under 2 minutes. This makes me so happy.

Here’s how to do it if you’re using RSpec with a standard project setup.

  1. gem install Simplecov
  2. Open your spec_helper.rb file (or whatever your RSpec entry point is) and enter the following at the very top. The call to SimpleCov.start must appear before any of the requires for your own code.
    require 'simplecov'
    
    SimpleCov.start do
      add_filter spec/
    end
    

  • If you’re using git, add the following line to your .gitignore file at your repository root.
    coverage
    

  • Run your tests as normal. The final line of your test run will be a message from SimpleCov alerting you to a newly generated coverage report.
  • Open the file coverage/index.html to view your coverage statistics.
  • It really couldn’t be simpler.

    About the author

    Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

    For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

    Twitter GitHub Facebook Instagram dirv.me