-
Notifications
You must be signed in to change notification settings - Fork 553
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
SimpleCov showing incorrect coverage result when parallelize is enabled in Rails 6.0.0 beta3 #718
Comments
@alkesh26 I don't have a solution for you but have link to my app and reproducible steps:
|
Yes this is a biggie. Sorry for taking so long to respond, I took a bit of a break from maintaining simplecov. Making simplecov completely work with rails and its new parallelization will be a bigger undertaking I fear. PRs are welcome. I'll see if I can allocate the time for it but I'm unsure. Thank you for reporting 💚 |
SimpleCov does not support running tests in parallel yet, a feature introduced only recently by Rails 6 [1]. Since our test suite is still quite small, and we expect it to stay like this for a while, we've disabled parallel tests. [1]: simplecov-ruby/simplecov#718
SimpleCov does not support running tests in parallel yet, a feature introduced only recently by Rails 6 [1]. Since our test suite is still quite small, and we expect it to stay like this for a while, we've disabled parallel tests. [1]: simplecov-ruby/simplecov#718
SimpleCov does not support running tests in parallel yet, a feature introduced only recently by Rails 6 [1]. Since our test suite is still quite small, and we expect it to stay like this for a while, we've disabled parallel tests. [1]: simplecov-ruby/simplecov#718
As a workaround we can piggy-back on the already existing feature of result merging, we just need to trick SimpleCov that each parallel run is another Command. We set
|
@bbascarevic-tti doesn't work for me, Are you running test with |
@bbascarevic-tti @chrisdebruin This doesn't work for me either. I'm also using |
The solution by @bbascarevic-tti is almost there. Here's what I ended up with; it seems to be working as expected: if ENV['COVERAGE']
require 'simplecov'
SimpleCov.start 'rails'
end
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
module ActiveSupport
class TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
if ENV['COVERAGE']
parallelize_setup do |worker|
SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}"
end
parallelize_teardown do |worker|
SimpleCov.result
end
end
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
end There are a few changes from @bbascarevic-tti's solution:
Here's an example output:
I've run the wipe / re-run without Spring many times and receive a consistent result so I don't believe there are any race conditions in the process model. When you mix in Spring, you get the lovely problems inherent in such a venture, so YMMV if you choose to try it with Spring. ThreadsNote that I've only been able to produce completely consistent results using process parallelization. When I tested out Next StepsI haven't been able to figure out a way to upstream this workflow into SimpleCov, so if someone has any bright ideas, please feel free to do that! The way |
@michaelherold I think you're close! I'm running into an issue though and maybe it's just general SimpleCov config and not due to parallelize? Regular tests
System tests run right afterwards
Regular tests again run right after the system tests Regular tests
It seems like it's not merging my system tests when I run EditI just had to put |
Hey everyone, thanks all of you for your work on this and exchanging ideas helping each other and laying important ground work 👏 I don't have a lot of time to invest into this right now (sadly), however I submitted a simplecov project to RubyTogether and this is on the list of things to work on/fix should the project get accepted. |
And as a small update, I did get the ruby together fund and hence an improved rails support especially its new test parallelization is towards the top of the list (after branch coverage and some friends though) |
* Fix simplecov clobbering test coverage reports when using parallel tests. * Generate coverage reports by default (remove $SIMPLECOV flag). * Store coverage reports in tmp/coverage/ instead of coverage/. * Enable branch coverage. ref: github.com /simplecov-ruby/simplecov/issues/718#issuecomment-538201587
@PragTob what's the status on this one? Disabling the parallelize feature of Rails is Edit: while disabling |
@AxelTheGerman hey, well - in the end as it's always the case with projects it didn't make the cut in the end. There were more branch coverage issues to iron out (and even more now that I finally used it in a project myself). So it's kinda a question of "when I get to it". Truth be told, due to other circumstances I'm not spending too much time on OSS these days. I might bite down some time during a vacation and do it, however, I think I'd first work on polishing existing features (branch coverage...) and bugs. As I'm sure, the parallelize implementation will bring many more fun bugs for me to deal with. Such as #815. Not the answer you were looking for I wager, but sadly the honest one I can give :) |
@PragTob thanks for all the awesome work you do! I also agree on making existing features robust first - as well as taking care of bugs and tech debt. I am fine without parallelize support but for now I haven't figured out how to even turn it off properly. If there is a workaround to get rid of parallelization in a Rails 6 app that'd be great. All I want is running regular tests and system tests with combined coverage - which should be possible with I have to check some of my other projects and see what might be different. Will report back when I figure it out :) |
I think the problem was either that I still had |
Omit loading
|
simplecov-ruby/simplecov#718 Rails6のパラレルテスト対策
This may be common knowledge, but make sure you're also eager_loading your testing environment. I followed the above advice, but still had some accuracy issues until I discovered this. Just run with # config/environments/test.rb
Rails.application.configure do
# many more configs...
# Use the CI EnvVar to control this
config.eager_load = ENV["CI"].present?
# some more configs...
end |
Any interest in maybe documenting @michaelherold's (excellent! working!) approach in the README, until/unless built-in support for Rails parallelization is added? Now that Rails will only start parallelizing tests once you hit 50 of them, the fact coverage will suddenly go from correct to near-0% is really confusing and takes time to piece together by searching issues in the repo. (The optimization is totally valid, but when a working initial setup stops working suddenly it can be disorienting.) |
For some reason, I still can't get SimpleCov to report correct percentages - e.g. it's reporting 0% coverage on This is our # frozen_string_literal: true
if ENV['CI']
require 'simplecov'
require 'simplecov-cobertura'
require 'minitest/reporters'
SimpleCov.start 'rails' do
command_name = ENV.fetch('CI_JOB_NAME_SLUG', nil) # rubocop:disable Lint/UselessAssignment
add_filter '/app/admin'
add_filter 'vendor'
add_group 'Policies', 'app/policies'
formatter SimpleCov::Formatter::CoberturaFormatter
end
Minitest::Reporters.use!([
Minitest::Reporters::DefaultReporter.new,
Minitest::Reporters::JUnitReporter.new('test/reports', true, single_file: true)
])
end
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'webmock/minitest'
require 'sidekiq/testing'
module ActiveSupport
class TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
if ENV['CI']
parallelize_setup { |worker| SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}" }
parallelize_teardown { |_| SimpleCov.result }
end
teardown do
Redis.new(url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/1')).flushdb
end
# Add more helper methods to be used by all tests here...
end
end
# Disable external requests
WebMock.disable_net_connect!(
allow_localhost: true,
allow: ['https://googlechromelabs.github.io', 'https://edgedl.me.gvt1.com']
)
# Use OmniAuth in test mode
OmniAuth.config.test_mode = true
Sidekiq::Testing.fake! This is how we collate the results namespace :coverage do
task report: :environment do
require 'simplecov'
SimpleCov.collate Dir['coverage/.resultset*.json'], 'rails' do
formatter SimpleCov::Formatter::HTMLFormatter
end
end
end |
Rails version: 6.0.0 beta3
Ruby version: 2.6.1
Issue
We have configured our test helper file as below
On executing
rake test
the coverage report is incorrect even when test cases are written of the method. So this is our user modeland the test cases we have are
But the coverage report when
parallelize
is enabled is as belowand when it is commented out the test results are all green
I tried below approaches but all of them give correct report only when
parallelize
is commented:.simplecov
file in root directory and executedrake
.But I am still not able to get the correct coverage result when
parallelize
is enabled.Many thanks in advance.
The text was updated successfully, but these errors were encountered: