9to5 coverage · Docs

Ruby code coverage with SimpleCov

Generate a single LCOV report from Ruby tests and upload it to 9to5 coverage from GitHub Actions.

Add SimpleCov and the LCOV formatter

Add these gems to your test group and commit Gemfile.lock after running bundle install. The example uses RSpec; Minitest can use the same coverage setup in its test helper.

group :test do
  gem 'simplecov'
  gem 'simplecov-lcov'
end

Start coverage before loading application code

Put this at the top of spec/spec_helper.rb or test/test_helper.rb, before requiring your application. In Rails, ensure it runs before the environment is loaded. Adjust tracked source directories to match the repository.

require 'simplecov'
require 'simplecov-lcov'

SimpleCov::Formatter::LcovFormatter.config do |config|
  config.report_with_single_file = true
  config.single_report_path = 'coverage/lcov.info'
end
SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
SimpleCov.start do
  track_files '{app,lib}/**/*.rb'
  add_filter '/spec/'
  add_filter '/test/'
end

Run tests and upload the report

- uses: ruby/setup-ruby@v1
  with:
    bundler-cache: true
- run: bundle exec rspec
- uses: 9to5/9to5-coverage-action@v1
  with:
    token: ${{ secrets.COVERAGE_UPLOAD_TOKEN }}
    path: coverage/lcov.info

Add these steps after checkout in your GitHub Actions job and select the Ruby version used by the project. Run on the default branch and PRs. Confirm coverage/lcov.info exists and contains the intended app/lib files before uploading.

Avoid incomplete coverage

Starting SimpleCov after loading Rails or your library misses already-loaded code. Parallel test jobs require deliberate collation of their results before generating the final report; this single-process example does not merge CI shards. Keep source paths relative to the checkout.

SimpleCov documentation · LCOV formatter configuration