Test Coverage with SimpleCov

The instructions at the GitHub repo for this gem are good, but they address the general use of SimpleCov, and I learned in testing that the order and location of some code makes a major difference in correctness of the output.

Note: I tested these instructions with Ruby 2.6.3 and Rails 5.1.7.  Although the gem is being actively maintained, it may not work the same way under Rails 6.  SimpleCov works with all common testing frameworks for Ruby on Rails.  I tested it with MiniTest.

Installing and Basic Use

1) Create a new branch with git just in case you run into problems with installation!!

2) Add the gem to your Gemfile by including this line somewhere outside of your  groups:

gem ‘simplecov’, require: false, group: :test

3) Run bundle install to install the gem

4) Add code to your test/testhelper.rb file to start the gem before any other code when in testing mode:

require ‘simplecov’
SimpleCov.start ‘rails’

If found, when I was testing out the gem that these lines really need to be above everything else in testhelper.rb, and you want to run it with the ‘rails’ option so that it checks all of the files involved in a Rails app.

5) Run the full test suite with rails test

6) Locate and open the coverage/index.html file to see the results.

7) Add the coverage directory to your .gitignore file so that you are not uploading it to the GitHub repo.

Enable Branch Coverage Testing

In the test/testhelper.rb file, use these options to turn on branch testing for all of your Ruby on Rails files:

require ‘simplecov’
SimpleCov.start ‘rails’ do
enable_coverage :branch

The views and opinions expressed on individual web pages are strictly those of their authors and are not official statements of Grinnell College. Copyright Statement.