Ruby Logo

Test Coverage & SimpleCov

Track coverage metrics, configure SimpleCov groups, and interpret reports to target high-impact tests.

Home Ruby Test Coverage & SimpleCov

Test Coverage & SimpleCov

Coverage metrics help us focus testing effort, highlight dead code, and track regressions. SimpleCov integrates seamlessly with both Minitest and RSpec.

Configure SimpleCov

# Gemfile group :test do gem 'simplecov', require: false end # test/test_helper.rb (or spec/spec_helper.rb) require 'simplecov' SimpleCov.start 'rails' do enable_coverage :branch minimum_coverage 90 add_group 'Services', 'app/services' add_group 'Queries', 'app/queries' add_group 'Jobs', 'app/jobs' add_group 'Controllers', 'app/controllers' end

Use branch coverage to catch missing conditionals. Adjust `minimum_coverage` carefully—enforce attainable targets (e.g., 90%) and build culture around meaningful tests, not gaming metrics.

Interpreting coverage reports

HTML report

Open `coverage/index.html` to see per-file metrics, branch misses, and hotspots. Lines highlighted in red need tests; yellow indicates partial coverage.

Trending metrics

Export coverage summaries as JSON (`SimpleCov::Formatter::JSONFormatter`) and push to Datadog or GitHub Checks for trend tracking in CI.

Strategy for meaningful coverage

  • Prioritize business-critical modules (`app/services`, `app/queries`). We group them in SimpleCov to keep the dashboard actionable.
  • Exclude auto-generated files (`config/`, `db/schema.rb`, vendor code) to prevent noisy metrics.
  • Set a threshold delta in CI—fail builds if coverage drops by more than 0.5% compared to main.
  • Use branch coverage to detect missing `else` clauses or guard branches.

Automation tips

  1. Run coverage on nightly builds to keep feedback fast for local TDD loops; optional in every PR if performance permits.
  2. Upload coverage from parallel CI runs using `simplecov-cobertura` to merge results.
  3. Surface coverage failures prominently in PR templates so reviewers can request missing tests before merge.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn test coverage & simplecov

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!