Created
June 9, 2025 01:39
-
-
Save oddboehs/8ca545a23fa7edd509947ff09951c67c to your computer and use it in GitHub Desktop.
SimpleCov Coverage Report Parser - Extract and display line and branch coverage from SimpleCov HTML reports in human-readable format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'nokogiri' | |
require 'set' | |
# Path to the SimpleCov HTML report | |
coverage_file = File.join(Dir.pwd, 'coverage', 'index.html') | |
unless File.exist?(coverage_file) | |
puts "β No coverage report found at #{coverage_file}" | |
puts "Run tests first: bin/rails test" | |
exit 1 | |
end | |
# Parse the HTML file | |
doc = Nokogiri::HTML(File.read(coverage_file)) | |
# Extract overall line coverage | |
line_percent = doc.css('.covered_percent .green').first&.text&.strip | |
total_lines = doc.css('.t-line-summary b').first&.text&.strip | |
covered_lines = doc.css('.t-line-summary .green b').first&.text&.strip | |
missed_lines = doc.css('.t-line-summary .red b').first&.text&.strip | |
# Extract overall branch coverage (first t-branch-summary is the overall stats) | |
overall_branch_summary = doc.css('.t-branch-summary').first | |
branch_percent = overall_branch_summary.css('span').last.text.strip.gsub(/[()]/,'') | |
branch_summary_spans = overall_branch_summary.css('span b') | |
total_branches = branch_summary_spans[0]&.text&.strip | |
covered_branches = branch_summary_spans[1]&.text&.strip | |
missed_branches = branch_summary_spans[2]&.text&.strip | |
# Extract timestamp | |
timestamp = doc.css('.timestamp .timeago').first&.attr('title') | |
puts "π SimpleCov Coverage Report" | |
puts "Generated: #{timestamp}" | |
puts "" | |
puts "π Line Coverage: #{line_percent}" | |
puts " β #{covered_lines}/#{total_lines} lines covered" | |
puts " β #{missed_lines} lines missed" | |
puts "" | |
puts "π³ Branch Coverage: #{branch_percent}" | |
puts " β #{covered_branches}/#{total_branches} branches covered" | |
puts " β #{missed_branches} branches missed" | |
puts "" | |
# Show file-by-file breakdown if there are missed lines or branches | |
if missed_lines.to_i > 0 || missed_branches.to_i > 0 | |
puts "π Files with missing coverage:" | |
puts "" | |
files_shown = Set.new | |
doc.css('tbody .t-file').each do |row| | |
file_name = row.css('.t-file__name a').first&.text&.strip | |
line_coverage = row.css('.t-file__coverage').first&.text&.strip | |
branch_coverage = row.css('.t-file__branch-coverage').first&.text&.strip | |
# Only show files that aren't 100% covered and haven't been shown yet | |
if !files_shown.include?(file_name) && (line_coverage != "100.00 %" || branch_coverage != "100.00 %") | |
files_shown.add(file_name) | |
puts " #{file_name}" | |
puts " Line: #{line_coverage}, Branch: #{branch_coverage}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SimpleCov Coverage Report Parser
A Ruby script that extracts and displays SimpleCov coverage data in a clean, human-readable format.
Features
Usage
Sample Output
Requirements
coverage/index.html
)Integration with Rails Testing
Add to your
test/test_helper.rb
to automatically show detailed coverage after test runs:This provides immediate visibility into which files need more test coverage!