Ruby Coverage
Framework Configuration
Key Requirements
- Coverage report must be in LCOV format
- Report must be saved as
coverage/lcov.info - Report must be uploaded as a GitHub Actions artifact named
coverage-report
RSpec is the most popular testing framework for Ruby, and SimpleCov is the standard tool for measuring code coverage. With the simplecov-lcov formatter, you can easily generate LCOV reports compatible with GitAuto.
Installation
source 'https://rubygems.org'
group :test do
gem 'rspec'
gem 'simplecov'
gem 'simplecov-lcov'
endSimpleCov Configuration
require 'simplecov'
require 'simplecov-lcov'
# Configure LCOV formatter
SimpleCov::Formatter::LcovFormatter.config do |c|
c.report_with_single_file = true
c.single_report_path = 'coverage/lcov.info'
end
# Use LCOV formatter
SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
# Start SimpleCov with filters and branch coverage
SimpleCov.start do
add_filter '/spec/'
add_filter '/test/'
enable_coverage :branch # Enable branch coverage tracking
end
# Your other test configuration...Setting Up GitHub Actions
Create a workflow file in .github/workflows/ directory. The filename can be anything you prefer (e.g. ruby-coverage.yml). Add the following content to your workflow file:
name: RSpec Coverage
# Run on target branch (probably default branch like main) to track coverage history
on:
push:
branches:
- main
# Optional: Only run when relevant files change (customize as needed)
paths:
- '**/*.rb'
- Gemfile
- Gemfile.lock
pull_request:
branches:
- main
paths:
- '**/*.rb'
- Gemfile
- Gemfile.lock
- '!.github/workflows/**'
workflow_dispatch:
# Auto-cancel outdated runs on the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true
- name: Install dependencies
run: bundle install
# PR: tests only, Push: tests with coverage
- name: Run tests
if: github.event_name == 'pull_request'
run: bundle exec rspec
- name: Run tests with coverage
if: github.event_name == 'push'
run: bundle exec rspec
env:
COVERAGE: true
- name: Upload coverage reports
if: github.event_name == 'push'
uses: actions/upload-artifact@v6
with:
name: coverage-report
path: coverage/lcov.infoKey Configuration Points
- Install SimpleCov and simplecov-lcov gems
- Configure SimpleCov to generate LCOV format reports
- Upload the report as an artifact - name must be either
coverage-reportor end withlcov.info - Ensure the artifact contains
coverage/lcov.infofile
Why !.github/workflows/** on PRs Only?
On pull requests, we exclude workflow file changes to keep the setup PR minimal. When you're adding or editing a workflow file, pre-existing test failures in your codebase are irrelevant and shouldn't block the PR. On push(after merge to main), we don't exclude them so that the initial coverage report gets generated on your target branch.
Viewing Coverage Reports
After your workflow runs successfully, GitAuto automatically processes the coverage reports and displays them in the Coverage Dashboard. GitAuto only saves coverage data when the workflow runs on your target branch (configurable in your repository's Rules page, defaults to your repository's default branch, e.g., main or master). This typically happens when:
- You merge a pull request to your target branch
- You push directly to your target branch
- You manually trigger the workflow
About LCOV:LCOV (Linux Code Coverage) is a standard format for code coverage data. It's pronounced "el-cov" and is widely supported by various tools and services.
Using multiple languages? If your repository has multiple programming languages (the docs use PHP + JavaScript as an example, but any combination works), see our Multi-Language Coverage guide for setting up coverage across all languages.
RSpec Coverage Issues?
Ruby testing can be challenging with gem conflicts, load order issues, and coverage gaps. Whether SimpleCov isn't running, coverage is inaccurate, or your CI is failing, we're here to help!
Contact us and let's get your Ruby tests shining!