We are trying to use simplecov with rspec from within a rake task. we have tried in Windows and ubuntu, but it always shows 0.0%, like this:
root@ip-10-81-10-196:~# rake
/opt/rvm/rubies/ruby-1.9.2-head/bin/ruby -S rspec test_spec.rb
.
Finished in 0.00039 seconds
1 example, 0 failures
Coverage report generated for RSpec to /root/coverage. 0 / 0 LOC (0.0%) covered.
we are not using bundler, we installed simplecov 0.5.2 and rspec 2.6.0 via "gem install".
rakefile.rb has:
require 'simplecov'
SimpleCov.start
require 'rake'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = '*_spec.rb'
end
task :default => :spec
and test_spec.rb has:
require 'rspec'
class TestClass
attr_reader :text
def initialize(text)
@text = text
end
end
describe "a simple test" do
it "should be covered" do
test_class = TestClass.new('default text')
test_class.text.should == 'default text'
end
end
How do we make it work?
thank you so much,
Isel
i was able to make it work with cucumber, but not yet with rspec...
I think rake runs tests in separate processes. For example, if you run rake test on a Rails app, you will get the coverage report generated each time a test suite is done (test:units, test:functionals, test:integration). You'll have do the setup of SimpleCov in your features/support/env.rb and spec/spec_helper.rb at the very top line. Otherwise, SimpleCov can't see your coverage from Rake.
If you use SimpleCov for multiple test tools, use the .simplecov file in the root, which should contain
SimpleCov.start 'rails' do
# whatever other config
end
Then, in env.rb and spec_helper.rb, just add require 'simplecov' at line 0, it should automatically pull in the config file. See the README, there is some information on this there.
we tried running just rspec from the cmdline, and yes we do have:
require 'simplecov'
SimpleCov.start
at the top of our specspec_helper.rb which we include as the first line in each test.
it is still showing 0%...
i managed to trace why i am not getting results. somehow it loads the adapter "root_filter" from the defaults.rb file and that adds a BlockFilter for the root of my project. i tried adding a .simplecov file but no luck. that filter stays there even if i load the 'test_frameworks' adapter...
just to get coverage working i tried this and it worked
module SimpleCov::Configuration
def clean_filters
@filters = []
end
end
SimpleCov.configure do
clean_filters
load_adapter 'test_frameworks'
end
Thanks @isel
I had the same problem, but adding @filters = [] to my simplecov configuration block made it properly detect my project instead of giving me 0 / 0
i found that i had to move my require 'simplecov' and SimpleCov.start block before require 'minitest/autorun'
FWIW this might also because your have in environments/test.rb
config.cache_classes = true
it should be set to false.
In my case the problem was that inside my test file oneclass_spec.rb, I was first require 'oneclass', and then require 'spec_helper'. Swapping the order solved it for me, ie. First require 'spec_helper' and then require 'myclass' .
Had the same issue. What my spec_helper.rb looked like:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
require 'sidekiq/testing'
require 'simplecov'
SimpleCov.start 'rails'
...
it produced no coverage. Move the include to the top, and getting proper coverage data now:
require 'simplecov'
SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
require 'sidekiq/testing'
...
I'm using Simple Cov without any framework just plain ruby classes. I wanted to see how the coverage was and I had the 0 lines covered issue.
I was able to solve this mixing @isel, @jokeyrhyme and @ahamid solutions. Here's my example:
require 'simplecov'
SimpleCov.start do
@filters = []
end
require 'minitest/autorun'
I needed to set the filters to an empty array and also require simplecov before requiring minitest/autorun.
Hope this helps anyone.
I was trying to be clever and requireing my library in its own gemspec, in order to get version and description information, so the whole library would be loaded before simplecov started. Fix was to require just the file with the version and description constants.
I have tried all mentioned solutions but none of them worked, I run below command before running RSPEC and it worked:
set JRUBY_OPTS="--debug"
Most helpful comment
FWIW this might also because your have in environments/test.rb
config.cache_classes = true
it should be set to false.