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
simplecovandsimplecov-lcovto the test group in your Gemfile. - Call
SimpleCov.startat 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/'
endGotchas
- If
SimpleCov.startruns after your code is required, those files show as uncovered. - When tests run in parallel processes, use
SimpleCov.command_nameper process and merge with SimpleCov result merging.
Related guides
How to Exclude Files From Coverage in CIKeep generated code, tests, and vendored files out of coverage numbers with per-tool exclude patterns so the…
How to Upload Coverage to Coveralls in CIReport coverage to Coveralls from CI with the coverallsapp/github-action, sending an lcov file with the GITHU…
How to Combine Coverage From a Matrix or Sharded Run in CIMerge coverage from parallel matrix or sharded CI jobs by uploading each shard as an artifact, then combining…