-
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
Covered code shown as skipped #60
Comments
I've also noticed something similar. For me though, it's a hit or miss thing. When I am working on a specific feature file and I run the test for that feature, it shows as everything 100% tested. Then, when I run all the features and specs, a method which was previously shown as 100% tested, now has some parts shown as untested. Then, when I rerun that specific feature again, it switches back to 100%. Weird behavior. |
I suspect this has to do with the way the test stack is loaded - if some part of your code is loaded before SimpleCov.start, it will not end up in the coverage report. What test frameworks are you using and how do you fire up the test suites (Rake task, specific command, ...)? |
I'm using rspec. I've SimpleCov.start 'rails' right after require statements in spec_helper.rb. So, loading order is not a problem i guess. I use bundle exec rspec spec/models to fire up model test suites. |
I'm seeing the same issue with ruby 1.9.2-p180, Rails 3.0.9, and simplecov 0.4.2. |
That's really odd. Guys, could you please try: 1. Update to latest dev release Update your
The latest dev version sports quite a few fixes and refactorings and I'm curious what the results are on that version. 2. Check out cached resultset Please find a line that is clearly executed in your tests but reported as uncovered in the HTML report. Then check out Further things:
Thanks! |
Well, I tested a bit more and my problem seems that when I type 'rake test' coverage results from the unit tests are overwritten by the ones from the functional and integration tests instead of accumulated. I guess also integration test results overwrite functional test results. |
What does the HTML report state in the footer as the sources for the report? Normally it should be "Unit Tests, Functional Tests, Integration Tests". Are you using Test::Unit 2? To fix that issue temporarily please see the section on customizing the command_name in the readme. You'll have to do SimpleCov.command_name 'Functional Tests' (Unit Tests, Integration Tests...) for each test suite in one of the _test.rb files in that given suite (it's not important which one, just be sure to have that for each of the test suites) |
It only says Generated by simplecov v0.5.0 and simplecov-html v0.4.5 in the footer. Using Test unit 2.3.2 |
Ok, so this obviously results from the problems described in #45 - test unit 2 messes with ARGV and thus SimpleCov cannot detect the test suite name properly. Please use the workaround mentioned above until I find a way to handle this automatically, sorry for the trouble! |
Yeah, no problem, I will try the workaround, doesn't seem so complicated. ;-) |
The workaround seems to work, the coverage percentage looks much more realistic now, however it says "Coverage report generated for test:functionals, test:integration" So it seems it still ignores the unit tests, although I put SimpleCov.command_name 'test:units' in one of my unit test files. |
Any updates on this? Wonder if my unit tests are included. but it seems so. although it only says "generated for test:functionals, test:integration" in the coverage report. I think it is because unit tests are somehow included/recognized as integration tests. The output from rake:test after running the unit tests is: Coverage report generated for Integration Tests, test:functionals, test:integration, test:units to ... |
I have the same issue. Rails 3.0.10 && Ruby 1.9.2p180, simplecov 0.4.2. Using autotest I see just the method declaration highlighted in green and no code executed (red). However, i tried rake just now and it works as expected. require 'simplecov'
SimpleCov.start 'rails' do
add_filter 'vendor'
end
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha'
require 'factory_girl'
require 'active_resource/http_mock'
require 'webmock/test_unit'
class ActiveSupport::TestCase I guess i could update autotest too and such. my final coverage report using rake: Test run options: --seed 13796
Coverage report generated for -e , Unit Tests, Functional Tests, Integration Tests to /Users/whoami/current_project/project/coverage. 1153 / 1673 LOC (68.92%) covered. |
I had this problem, too, until I realized that it could be something related to the require 'simplecov'
SimpleCov.start in my |
I also came across the same weird behavior (using rspec btw.). In my case none of the workarounds seem to help. In fact: Adding one line of Capybara code breaks the code coverage of an (almost) completely unrelated code. To reproduce the bug:
At this point you'll notice, that the model
in I'm on Mac OS X 10.6.8 and my Ruby version is:
BTW. The name of the repository might be a bit insulting. I don't mean it, it's still a great gem! :) |
I spent some more time figuring out what's going on and it seems that FactoryGirl is somehow involved in this issue. I don't get that bug if I use All simplecov tests pass on my machine, btw. |
Ah, finally some code to try out. Thanks @timhabermaas - I'll check it out over the weekend. |
Ok, I checked out @timhabermaas' sample project, and the problem clearly lies in Ruby's require 'coverage'
Coverage.start
at_exit do
require 'pp'
pp Coverage.result.find{|file, cov| file =~ /competition\.rb/ }
end
I'm not sure why this happens, but I'll investigate this further and will file a bug for Ruby once I have narrowed this down. |
Ok, I filed a bug on Ruby 1.9 here: http://redmine.ruby-lang.org/issues/5333 I also forked and documented the project submitted by @timhabermaas at https://github.com/colszowka/coverage-bug I guess now we'll have to wait and see where this brings us. Thanks for the patience folks :) |
Is this related to FactoryGirl? I converted my rails app from using fixtures to FactoryGirl and coverage has been 0/0 since then. I changed a bunch of stuff so I can't say that exactly it though. I guess let's all the useful information: rails (3.1.0), mac os lion, rspec (2.6.0), simplecov (0.4.2), ruby 1.9.2 |
@LegoMaster: Not sure, but in the sample provided by @timhabermaas it is. I'm not sure why this happens as from what I know FactoryGirl does not do any black magic, so it's weird. Also, I always use factory girl on my projects and haven't had this issue myself yet - but I mostly use Test::Unit, so it might be a problem with RSpec in conjunction with FactoryGirl. Still, it seems awkward the Coverage lib doesn't report code that clearly has been executed. |
It's very well I've messed up the configuration or something in my stuff, I'll keep digging. @timhabermaas seems to be getting coverage, just not correct coverage. I'm not getting anything at all currently. Thanks! |
Hello, thank you all for tracking this issue. I'm an author of coverage.so. |
Hi @mame, oh, that's really interesting, didn't figure yet this might be a problem on OS X only... I just tried it out on OS X so far, but I will try it on Ubuntu too to approve it works there. |
Thanks @mame for looking into it. |
I might do something wrong to reproduce the bug because I'm not familiar with rails, rake and rspec :-) |
After investigation with @nagachika and @nobu, the same file is reloaded twice, which forces to reset the coverage. So this is not a bug of coverage.so. I wrote the detail in http://redmine.ruby-lang.org/issues/5333 |
Having the same problem. I rolled back through my commits to find where this started happening and it very clearly started when I changed from Rails 3.1.0.rc1 to the released version of 3.1.0. The only change in that commit was updating the Gemfile to the newer version of Rails (and the related changes to Gemfile.lock), but there's a very stark contrast to what's being "covered" before and after that commit. Now on 3.1.1 and the problem persists. So, it might be a Rails issue with excessive reloading of files. |
@jsgarvin Are you using RSpec? I suspect this only happens with Rails + RSpec, for whatever reason. I bugged @josevalim and @tenderlove about this on Twitter since I have no idea how to further investigate this issue :/ Hopefully they'll find the time to give some directions :) |
Has there been any movement on this issue, or an alternate workaround? I tried requiring my model in my spec:
but this didn't affect the output of the entire suite. However, I can confirm that when only running one spec:
The generated coverage report is accurate. Rails 3.1, Rspec, FactoryGirl, OSX and with/without Guard & Spork. |
@jaryl No difference for me whether I run my rspec suite with:
or
|
First of all thanks a lot @tenderlove for taking the time and investigating this! Hopefully now that the root cause of this is known figuring out how to avoid it shouldn't be too far away. What I don't really understand is why Rails would need to do class reloads in the test environment. While auto-loading missing constants makes sense to me in regards to optimizing load time when running individual tests, having them reloaded once they're in the stack doesn't seem required to me. Anyone got an idea why this would be required? I'll have to dig further into this for further conclusions as I didn't have the time to look into this in the last weeks due to christmas/new year madness, real life and fracturing my shoulder. I'm sure not far from now we'll have a working solution for this big problem though. Next steps/ideas include:
|
@smathson I think in line with what Aaron said you'll need to require your controllers since at some point they might be reloaded. Try to add the |
Ok, had a look at the Rails 3.1 source files Aaron mentioned (https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/action_dispatch/middleware/reloader.rb#L58-74 and https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/application/bootstrap.rb#L54-59) and compared them against Rails 3.2-stable - As I expected, there were quite a few changes to this. Most importantly, the I then went on to try the test repo (https://github.com/colszowka/coverage-bug) against Rails 3.2, and obviously this made things work! Both the Factory Girl and the ActiveRecord examples (see the README in the project there) now give me correct results for Coverage. This means that on Rails 3.2 at least, this problem should be resolved. It would be great if all of you who bumped into this problem could check whether this is the case for you. Still not sure what to do about Rails 3.0 / 3.1 though as probably a lot of people will be on those versions still. |
Turns out, forcing TL;DR: How to fix itOn Rails 3.0/3.1
On Rails 3.2Should work out of the box All Rails versions: Please report back as to whether this really works :) |
Awesome! Thanks very much @colszowka! That fix works brilliantly :) Nice that the problem has gone in 3.2 as well! |
@colszowka config.cache_classes = true fixes the issue for me as well. Thanks! |
Still seem to be having the same problem under 3.2.1, with config.cache_classes = true. Using Test::Unit, not Rspec. |
Putting
|
The above workaround that @jsgarvin suggested also worked for me. I'm using SimpleCov with Rspec and Spork on OS X Lion. This is how part of my spec_helper.rb looks like: Spork.each_run do
require 'simplecov'
SimpleCov.start 'rails'
SimpleCov.command_name
end |
I take what I said above back. :-) Using @colszowka's suggestion above doesn't work for me either because I use Spork which preloads Rails and my objects in the prefork block which means whenever I edit a model or any of my app classes, the tests will never use them. I'll try looking for other means... |
@relaxdiego For spork there appears to be a working solution at #42 now, please check that out. I'm not using it myself, but a couple people reported it as working. @jsgarvin You can safely remove the As I haven't heard new complaints on this and everyone who reported back got it to work I think this can finally be closed. For resolving problems with Spork please check out #42. |
I'm trying to use SimpleCov to measure unit test coverage for my Ruby code, and the results are clearly only counting lines which are run during initialization. For a method I'm not using Rake, Rails, or anything like that: just running a Test::Unit test suite directly from the command line! I'm using Ruby 1.9.2, on Windows Vista. |
To anyone who runs into the same problem when running |
Thank you @klauern you are the best, mate!! |
I'm also using simplecov on a rails app. with spork and I managed to get it working using a initializer:
For spork I tried doing the same as @relaxdiego, which does load simplecov but does not generate the coverage at the end. Still figuring that out. |
To anyone still struggling with this (using test/unit library and running the tests with rake):
|
I'm using ruby 1.9.2-p180, simplecov gem 0.4.2.
Though, i've written test for my model methods, simple coverage html file shows all of them as missed lines. This has happened only in few of my models and not all.
The text was updated successfully, but these errors were encountered: