simplecov icon indicating copy to clipboard operation
simplecov copied to clipboard

Reporting branch coverage percentage in the terminal output

Open DannyBen opened this issue 2 years ago • 2 comments

I am not sure if I am doing something wrong, or if this is how it behaves.

I would like my code to have full branch coverage, but I am unable to configure Simplecov to report branch coverage percentage. I am referring to the percentage reported at the terminal:

Coverage report generated for RSpec to /vagrant/gems/runfile/coverage. 453 / 457 LOC (99.12%) covered.

I am able to see the Branch Coverage column and details in the HTML report, but I would like to see it in the terminal output as well.

image

Furthermore, the primary_coverage :branch setting - which I thought is the solution - does not seem to have any impact for me whatsoever. Not sure what is it supposed to do (and I have looked through all the pieces of Simplecov code that refers to it).

My top of the spec_helper.rb file looks like this:

require 'simplecov'

unless ENV['NOCOV']
  SimpleCov.start do
    enable_coverage :branch
    primary_coverage :branch
  end
end

Additional context

please ensure you are requiring and starting SimpleCov before requiring any application code.

Simplecov required and working at the top of spec_helper.rb

If running via rake, please ensure you are requiring SimpleCov at the top of your Rakefile

No rake. Directly running rspec, on a non rails app

Have you tried using a .simplecov file?

No

Include the SimpleCov version you are running in your report.

0.22.0

Include your ruby -e "puts RUBY_DESCRIPTION".

ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) +YJIT [x86_64-linux]

DannyBen avatar Jan 26 '23 11:01 DannyBen

This appears to be a limitation in simplecov-html, the default formatter: https://github.com/simplecov-ruby/simplecov-html/blob/22470072b687c60d11f488a5b86e239d1c7b4498/lib/simplecov-html.rb#L35

Here's a workaround:

SimpleCov.at_exit do
  SimpleCov.result.format!

  result = SimpleCov.result
  if result.total_branches&.positive?
    covered_branches_percent = 100.0 * result.covered_branches / result.total_branches
    puts "Branch coverage: #{result.covered_branches} / #{result.total_branches} branches (#{covered_branches_percent.round(2)}%) covered."
  end
end

noahfpf avatar Feb 10 '23 17:02 noahfpf

Thanks. Shouldn't the output_message method in the formatter be adjusted to do this?

DannyBen avatar Feb 10 '23 18:02 DannyBen