SimpleCov not processing statistics - ruby-on-rails

As the simpleCov gem documentation indicates, the test_helper.rb file was amended in a Rails 7 app to include
require 'simplecov'
SimpleCov.start 'rails'
puts "SimpleCov started successfully!"
# Previous content of test helper now starts here
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require 'webmock/minitest'
and a model test file
class CategoryTest < ActiveSupport::TestCase
puts "MyCode is being loaded!"
runs as expected
> rails test test/models
SimpleCov started successfully!
MyCode is being loaded!
However the net coverage is blatantly wrong:
93 runs, 192 assertions, 0 failures, 0 errors, 0 skips
51 / 3095 LOC (1.65%) covered.
as this was a copy of another application that had full coverage and was renamed.
I noticed at some point that the coverage percentages were dropping, but that was while running individual tests (and thus ignorable given SimpleCov re-writes all); now the result remains invariably at that figure.
The only thing I can think of is that some lines are covered and thus something might be interrupting SimpleCov's analysis somehwere down the line.
Any thoughts on how to debug this? [now moot question]
update
I had the itch about this some time ago and came across a solution.
Commenting out parallelize(workers: :number_of_processors) allows coverage to be measured.
So why is this default setting clashing with SimpleCov ?

Related

Rspec always exits with code 1 when simplecov is enabled

I have a Ruby on Rails 6.1 application I am testing with rspec 3.10, all tests passing and it exits with code 0. When I add Simplecov (using 0.21.2) the exit code is always 1.
My simplecov configuration:
# spec/rails_helper.rb
require 'simplecov'
if !ENV["NO_COVERAGE"]
SimpleCov.start :rails do
minimum_coverage 0
minimum_coverage_by_file 0
filesize = Proc.new { |source_file| source_file.lines.count < 10 }
add_group "Workers", "/app/workers"
add_group "Builders", "/app/builders"
add_group "Queries", "/app/queries"
add_group "Decorators", "/app/decorators"
end
end
Example output (from a Github action)
Finished in 4 minutes 18.1 seconds (files took 11.15 seconds to load)
2415 examples, 0 failures, 154 pending
Randomized with seed 29170
Coverage report generated for RSpec to /app/coverage. 8665 / 13736 LOC (63.08%) covered.
1
Error: Process completed with exit code 1.
If I remove or disable simplecov everything works fine. If I turn it back on, exit code is always 1. This happens on GH Actions, my dev machine and inside Docker containers.
Am I doing something wrong? How do I get simplecov to always return 0 since I don't care about minimum coverage anyway?

Customize minitest messages

Is there a way to customize test reports. Currently I am getting this line at the end of each test:
3 tests, 0 assertions, 0 failures, 0 errors, 0 skips
I want to include test file name to this line.
Minitest has a verbose option that will show the test name and execution time for each test that's run. How you pass the -v flag to Minitest will vary depending on how you're running your tests. See http://chriskottom.com/blog/2014/12/command-line-flags-for-minitest-in-the-raw/ for a sample of verbose output.
Another option would be using minitest-reporters with the SpecReporter by adding it to your Gemfile and putting this in your test helper:
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
See https://github.com/kern/minitest-reporters for more info.

RSpec is Freezing

I have rspec configured installed in my rails app. It was working fine (We are just experimenting with rspec so there's only 2 tests).
They were working fine. Now rspec is freezing when it's going to perform a test using database.
I just freezes. I don't even know were to start looking because there's no error in the output.
Is there a verbose or debugging mode for rspec or someone ever faced this?
I've tried -b but it freezes before can give an error.
Output: (Rspec is configured with --format documentation)
[leandro#machine:~] rspec
User
Than, that's it. It hangs. I has to reset manually the computer twice.
This is the user_spec.rb
require 'spec_helper'
describe User do
let(:user) { User.create({
username: "teste",
email: "teste#teste.com",
name: "Teste",
phone: "123456789",
password: "123456",
notes: "Teste"
}) }
subject { user }
it "is invalid without a username" do
user.username = nil
is_expected.not_to be_valid
end
end
And my spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.color = true
config.tty = true
config.formatter = :documentation #:progress, :html, :textmate
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
SOLUTION
It turns out that delayed_job_active_record gem was causing the hanging.
I don't know why, but as #Greg Malcolm I looked into the log/teste.log and rspec was feeezing right after createing the delayed jobs database and setting up a new user.
I've restricted the gem just for development and production enviroment, and it worked!
I haven't heard of a rake like verbose feature for rspec. But it's probably more useful to look through log/test.log and see what shows up there. It shows database activity which is part of a freeze effect.
You might also want to rails console into test and make sure your database connection is working right.
You have to check the ./log/test.log file. As this file tends to grow big, use the tail command to read the last n lines.
For example, to read the last 5 lines of the log file, execute the following command:
tail -5 ./log/test.log
In my case, my Google Pub/Sub emulator was hanging.
[ActiveJob] Failing with status #<struct Struct::Status code=4, details="Deadline Exceeded", metadata={}>
[ActiveJob] calling pubsub.googleapis.com:443:/google.pubsub.v1.Publisher/GetTopic
It might also be helpful to use tail -f ./log/test.log, to keep track of the logs while RSpec is running. You can read more about it on "Tail Your Test Logs", by thoughtbot.
It's pretty old question but in case anybody else will get this. I've got the same strange Rspec behaviour today because of left --drb option in project .rspec file after removing DRb server (spork or zeus). Just make sure Rspec have DRb disabled if you don't have spork or zeus installed.

No expected output for my performance testing

I want to do a performance test for my rails 3 app and I did a try according to the rails online guide
rake test:profile
and it gave some output as:
Specify ruby-prof as application's dependency in Gemfile to run benchmarks.
Loaded suite /usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
.
Finished in 0.326233 seconds.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips
But according to the guide, there should be something like:
BrowsingTest#test_homepage (31 ms warmup)
wall_time: 6 ms
memory: 437.27 KB
objects: 5,514
gc_runs: 0
gc_time: 19 ms
and also some log files produced in app's tmp/performance dir, which doesn't exist in my case.
The performance test is the generated sample test, browsing_test.rb, in my app's test\performance dir:
require 'test_helper'
require 'rails/performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
def test_homepage
get '/'
end
end
And my rails version is 3.0.10. Can anyone give me some tips or hints?
Had the exact same issue, and solved it by running
rvm install 1.9.2-p290 --patch gcdata # Make sure you have the same patch version
(note: not sure how much that step helped since I had an error, but since it's part of what I did...)
Adding to my gemfile
gem 'ruby-prof', :git => 'git://github.com/wycats/ruby-prof.git'
gem 'test-unit'
And of course running
bundle install
Some reading that helped me
http://rubylearning.com/blog/2011/08/14/performance-testing-rails-applications-how-to/
https://rails.lighthouseapp.com/projects/8994/tickets/4171-fresh-rails-3-app-cannot-run-performance-tests
http://guides.rubyonrails.org/performance_testing.html#installing-gc-patched-mri

Autotest: "Could not run tests". But nothing in the log

Autotest broke with rspec-rails 2.2.1 update. Now I can't get it working again.. .even after rolling back to rspec-rais 2.2.0.
Only info I'm able to find is a Growl popup saying "App: Could not run tests". Nothing is being written to log/test.log.
this is output in console:
/Users/meltemi/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit test/test_helper.rb].each { |f| require f }" | unit_diff -u
Loaded suite -e
Started
Finished in 0.003428 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 38767
Any ideas where to start troubleshooting this? I'd look in an autotest.log...if i could find one!?!
Edit: tried running autotest with autotest -v and get a looong list of "No tests matched..." in the console (20± examples below):
...
No tests matched .git/objects/fe/41f5cc24d667635c46a533fd09bf7bb514e032
No tests matched .git/objects/fe/4577696b2811818fe7439990be0d2f65a892c5
No tests matched .git/objects/fe/e16b09c5e782035a347ed9b257ff9b2b3fa173
No tests matched .git/refs/heads/MenuModel
No tests matched .git/refs/heads/master
No tests matched .git/refs/remotes/origin/master
No tests matched .git/refs/tags/v0.0.1
No tests matched .git/refs/tags/v0.0.1.1
No tests matched .gitignore
No tests matched .rspec
No tests matched Capfile
No tests matched Gemfile
No tests matched Gemfile.lock
No tests matched README
No tests matched Rakefile
No tests matched app/helpers/application_helper.rb
No tests matched autotest/discover.rb
No tests matched config/application.rb
No tests matched config/boot.rb
No tests matched config/database.yml
No tests matched config/deploy.rb
No tests matched config/environment.rb
...
here's the config (~/.autotest):
# ~/.autotest
# Include plugins
require 'autotest/fsevent'
require 'autotest/growl'
# ./.autotest
# exceptions (files to ignore)
# mappings (which files to specifically monitor for changes)
Autotest.add_hook(:initialize) {|at|
at.add_exception %r{^\.git} # ignore Version Control System
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
# at.clear_mappings # take out the default (test/test*rb)
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
Dir['spec/**/*.rb']
}
nil
}
autotest -s rspec2 tells autotest to use the "rspec2" style, which will make it look for spec files and run the examples therein.
This is just a workaround though. Autotest is supposed to discover the specs based on the entries in your project's ./autotest/discover.rb. And as you found, a recent code update broke the autodiscovery. I ran into this myself and was unable to track down the source.

Resources