用simplecov gem进行Ruby覆盖率测量是什么

bzzcjhmw  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(89)

当在Mac OSX上运行simplecov时,所产生的覆盖率几乎没有意义。
如果运行以下测试:

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.

然而,当运行rails test test/models时,图形输出指示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

更新我推测可能是测试所选的语法存在缺陷-我添加了补充测试,结果仍然报告

型号3 relevant lines. 0 lines covered and 3 lines missed.

class Channel < ApplicationRecord # red

  validates :name, presence: true # red

end # red

因此,测试通过,但覆盖率结果令人困惑:
a)作为独立测试,覆盖率计数为0/0,而测试通过
B)什么构成miss或相反coverage由a(the?)测试?
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

更新2根据@BroiSatse的建议,注解掉parallelize(workers: :number_of_processors)允许测量覆盖率。

nnsrf1az

nnsrf1az1#

谢谢,这个问题也是我的问题。所有的信贷BroiSatse的评论。
我在test_helper.rb中添加了以下代码行,它保持了并行测试的速度优势,而不是简单地注解掉这一行。它基于BroiSatse评论的链接:

# 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

在此之后,SimpleCov报告的测试覆盖率从3.8%“提高”到90%以上。

相关问题