Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporting branch coverage percentage in the terminal output #1051

Open
DannyBen opened this issue Jan 26, 2023 · 2 comments
Open

Reporting branch coverage percentage in the terminal output #1051

DannyBen opened this issue Jan 26, 2023 · 2 comments

Comments

@DannyBen
Copy link

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]

@noahfpf
Copy link

noahfpf commented Feb 10, 2023

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

@DannyBen
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants