3

While running simplecov on Mac OSX, the resulting coverage makes little sense.

If the following test is run:

rails test test/models/channel_test.rb
> 4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
> Coverage report generated for Minitest to /Volumes/[...]/coverage. 0 / 0 LOC (100.0%) covered.

Yet when running rails test test/models the graphical output indicates for test/models/channel_test.rb

require "test_helper" 
class ChannelTest < ActiveSupport::TestCase 
  test "invalid if name not defined" do  
    channel = Channel.new(priority: 1, unit_cost: 1, daily_limit: 9999) 
    assert_not channel.valid?  
    assert_not channel.save, "Saved the channel without a name"
  end   

update I presumed it might have been the chosen syntax of the test that might be a flaw - I added a supplemental test and the result is still reported
for the model 3 relevant lines. 0 lines covered and 3 lines missed.

class Channel < ApplicationRecord # red

  validates :name, presence: true # red

end # red

Thus the test is passing, but the coverage result is confounding:

a) as a standalone, the coverage count is 0/0, whereas the tests pass

b) what constitutes a miss or conversely coverage by a (the?) test ?

test_helper.rb

 require 'simplecov'
 SimpleCov.start

ENV['RAILS_ENV'] ||= 'test'
require_relative "../config/environment"
require "rails/test_help"
require 'webmock/minitest'


class ActiveSupport::TestCase
  parallelize(workers: :number_of_processors)


  fixtures :all

  def log_in_as(user, shop)
    post user_session_url, params: { user_id: user.id, active_shop_id: @site.shop_id }
  end

end

Update 2 As per @BroiSatse suggestion, commenting out parallelize(workers: :number_of_processors) allows coverage to be measured.

Jerome
  • 5,583
  • 3
  • 33
  • 76
  • please show us your test_helper file – BroiSatse Nov 18 '21 at 08:40
  • question has been edited to include test_helper. – Jerome Nov 18 '21 at 08:48
  • How does the `Channel` model look like? – spickermann Nov 18 '21 at 09:32
  • 2
    Firstly - change `SimpleCov.start` to `SimpleCov.start 'rails'` - your tests should not be part of the coverage. Secondly - this is most likely an issue with parallelization - simplecov can only able to detect line hits in the current process and parallelization might throw it off: https://github.com/simplecov-ruby/simplecov/issues/718#issuecomment-538201587 – BroiSatse Nov 18 '21 at 11:14
  • @spickermann model only validates `validates :name, presence: true`. – Jerome Nov 18 '21 at 13:45

1 Answers1

3

Thank you, this question was my issue too. All credit to BroiSatse's comment.

I added the following lines to test_helper.rb, which maintaines the speed benefit of parallelized tests instead of simply commenting out the line. It is based on the link from BroiSatse's comment:

  # Run tests in parallel with specified workers
  parallelize(workers: :number_of_processors)

  #combine SimpleCov results to accurately get results from parallelized tests
  parallelize_setup do |worker|
    SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}"
  end

  parallelize_teardown do |worker|
    SimpleCov.result
  end 

Following this my test coverage reported by SimpleCov "improved" from 3.8% to over 90%.

Nick Gobin
  • 131
  • 13