Skip to content
Latchkey

How to Generate SimpleCov Coverage in CI

SimpleCov must start before your app code loads, so it goes at the very top of the test helper.

Add SimpleCov.start as the first thing in spec_helper.rb or test_helper.rb, then add the LCOV formatter so CI can upload coverage/lcov.info.

Steps

  • Add simplecov and simplecov-lcov to the test group in your Gemfile.
  • Call SimpleCov.start at the top of the test helper, before requiring app code.
  • Enable the LCOV formatter and upload coverage/lcov.info.

spec_helper.rb

spec_helper.rb
require 'simplecov'
require 'simplecov-lcov'

SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::LcovFormatter,
])
SimpleCov.start 'rails' do
  add_filter '/spec/'
end

Gotchas

  • If SimpleCov.start runs after your code is required, those files show as uncovered.
  • When tests run in parallel processes, use SimpleCov.command_name per process and merge with SimpleCov result merging.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →