-
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 with minitest Unit tests? #235
Comments
Sorry i think i noticed, If I run each file individually then it shows green
But If run with
or
it does not work.. |
I was able to get it to work as follows: namespace :test do
task :coverage do
require 'simplecov'
SimpleCov.start 'rails' # feel free to pass block
Rake::Task["test"].execute
end
end Did not work when I dropped require 'simplecov' in test_helper |
Thanks @moger777 it works :) |
This should work when the load order is correct (test_helper -> simplecov & start -> actual app code). I am not sure how minitest handles this by default though, so there may be something simplecov can do differently to make things work out of the box, at the very least meaning improving the docs. I believe what also should work if you use Bundler and it automatically requires your code would be to use the |
I had set up a new Rails 4 app (Ruby 2) with four models and three controllers with some basic tests. When I added SimpleCov to the test_helper as described it generated data for unit tests, but nothing for controllers, helpers,libraries or my custom resque definitions in app/jobs. I also tried the change from the pull request #244 for addining minitest support, but it made no difference (seems it's just a text label?) By moving it into a separate Rake task as described by @moger777 above, I was able to run the coverage task and generate a report that included everything. |
This is definitely a load order issue in that the way Rails handles things with Minitest and the way RSpec sets up things with Rails is entirely different... Here is some other information:
|
Here is your workaround: https://gist.github.com/envygeeks/6629308 |
Thanks @envygeeks! works |
I'm using MiniSpec with Guard and have the same problem. Running stand alone generates just fine but running through guard shows all the method bodies being skipped. The railties fix above did not work for me. 13:56:55 - INFO - Guard::Minitest 1.1.0 is running, with Minitest::Unit 4.7.5!
13:56:55 - INFO - Running all tests
Coverage report generated for Unit Tests to /Users/brianp/Sites/myapp/coverage. 986 / 1718 LOC (57.39%) covered.
Loaded suite
Started
..........................................................................(continues)
Finished in 6.48651 seconds. |
i get this same problem when running a fresh rails 4 application with the |
Same thing here, seems to not work fine with either |
Same thing for me with using guard and/or minitest-spec-rails. @envygeeks, what was the workaround? Gist is removed 😢 |
@envygeeks it would be awesome to see your workaround! |
I don't know about @envygeeks workaround, but I found something that worked for me (I'm using |
I can't get it to work with Anyone aware of what exactly the issue is or how to work around this? |
@kopchickm 's solution does it for me. Thanks! |
I am still having trouble with this. What is the solution or workaround? Thanks in advance! |
This is a load order issue. SimpleCov must be required and started before any library code is loaded. This is a common problem when running tests via rake. See me comment at #314 (comment) |
I think one should read the comments before doing silly shit like closing a ticket with a broad statement that applies but cannot feasibly be fixed by the user without simplecov doing some work to actually make the issue fixable by the user. |
@envygeeks I apologize if I closed this erroneously. Would you mind summarizing what I'm missing? Still looks like a load order issue to me, but I'll happily be corrected and help out if you if wrong. |
@bf4 it is a load order issue, there is no doubt about that, but it's a load order issue that was rather hard for the average user to fix because even if we required simplecov before everything else in our test helper (the same way we would with RSpec) it did not actually get required first, it landed after everything else which required me to inject it and hack it in with a railtie, so that it was always loaded when Rails loaded it's test crap. While it was easy to fix, one of my initial suggestions was for Simplecov to provide the Railtie and then people wouldn't even need to much more than require that in their app where Rails does it's own requires. As my comment above (way above maybe) stated, that no matter what I did in my helper it wouldn't work right, I either had to have rake require it long before it did anything else, or I had to use a Railtie. |
Okay, so I think we're coming at this from different perspectives is all. I was saying that if you run your tests with rake, to ensure you require and start simplecov there, before any Rails code is required, rather than in a spec helper, whereas you're saying, since vanilla Rails apps require the app in the Rakefile, the 'convention over configuration' approach would be to work with Rails and provide a railtie. A railtie sounds like a fine idea, though I, personally, think the better path is for the docs to be clearer on this matter, and to recommend using a I did click on your gist link above when first reading these issues and it didn't resolve, so perhaps that's part of our miscommunication, as well. |
The gist is missing now. Anyone know where it can be found? |
@jonmarinello Do you have a bug or just curious? If a bug, please create a new issue. This issue is closed. |
Got it resolved. Thanks. |
Right, so what is the workaround for this? I'm using 'minitest-rails-capybara', and coverage misses most (but not all) of my model files... |
@elsurudo have you read the comments? simplecov must be started as early as possible. running via rake? require and start at the top before any app code is loaded. I'd recommend using a |
Yes, I've read them. That's the thing. I'm including
right at the top of my The strange thing is it picks up some of my models, but not all of them... |
I'm going to circle around to the issue I had with your statement but blew off because I didn't have time to care but this is the second time it's come up, how exactly is a .simplecov file going to solve load order issues with Rails and Minitest run through rake? I'm just as confused by the logic as I'm sure @elsurudo is. |
And yes, exactly. From my understanding |
That said, what I did and still do for all my newer Rails is just create a Railtie and then require it early in my module MyApp
module Railties
class SimpleCov < Rails::Railtie
config.before_initialize do
if Rails.env.test?
require "simplecov"
# If you need setup SimpleCov stuff here.
end
end
end
end
end |
@envygeeks thanks for helping out here. I agree the docs needs to be better etc. but no one has fixed it yet. So, first, background on how simplecov gets coverage, extracted from #314 (comment) Where coverage comes fromSimpleCov uses the stdlib Coverage module The Coverage module, when active, tracks when code is evaluated for the first time (i.e. when loaded or run ) SimpleCov starts and stops tracking coverage (via Coverage) when you run Thus, when you run Where
|
Your assumptions neglect a very important fact: Where the Railtie comes from. You are right that "I" don't need the |
Thanks for the help and thorough responses, gentlemen. It's gotten me on the right track, and it is now just a matter of configuring things... However, whenever I've used SimpleCov in the past, I dropped those two lines into my test helper, and I was good to go... I really think that the fact that you feel this issue requires a "standard response" is a problem in itself, since this is really the main use case of the gem. Shouldn't the 90% case warrant the simplest configuration possible? |
Ok @envygeeks good point. I was assuming you meant that code in your app-railtie should exist in the simplecov one. @elsurudo IMHO, the problem is in conflicting conventions. If someone is running tests via I created issue #340 and updated some more info in there.. much of it should be in the README, not in a STANDARD RESPONSE, I think. |
My mind tells me that the simplest solution is to upgrade the Railtie, have it require and detect a '.simplecov' file and load it or continue with a default Rails setup... then you tell people modify their Gemfile to It wouldn't be crazy to have SimpleCov detect |
@envygeeks no need, I don't think, for # Load Rails integration (only for Rails 3, see #113)
require 'simplecov/railtie' if defined? Rails::Railtie and the |
The first part is fair enough but your latter statement does not solve or even address the core of my suggestions for making simplecov easier. |
@envygeeks sorry, now I see what you're saying. Something like require 'simplecov'
config.before_initialize do
SimpleCov.start('rails') unless SimpleCov.running
end with maybe some guards or something around it. |
I have missing model coverage if I place the SimpleCov.start in my test_helper.rb (with Minitest). I use config.before_initialize now. |
The code coverage analysis tool did not correctly report test coverage results if executed via guard-minitest. Externalizing the config into `.simplecov` is considered best practice: simplecov-ruby/simplecov#235 (comment) The `.simplecov` config is optimized for Cloud Stove based on the Rails defaults from: https://github.com/colszowka/simplecov/blob/master/lib/simplecov/defaults.rb Positive side effect: Adding Spring support for MiniTest accelerates repeated test executions through reloading: https://github.com/guard/guard-minitest#spring The new Rake task `rake test:coverage` also triggers coverage analysis. SimpleCov is very fast (i.e., a couple of seconds for ~10 min Rails test suite) and thus they enable coverage analysis by default. For conditional execution, see: https://github.com/colszowka/simplecov#running-coverage-only-on-demand
Externalizing the config into `.simplecov` is considered best practice: simplecov-ruby/simplecov#235 (comment)
FWIW, in my simple gem it was relatively easy to get SimpleCov going.
I guess further config would go into |
There is already generous documentation in the README I feel. Your step number 5 is framework specific. Also what is mentioned in 3. needs to be required somewhere. PRs to improve the documentation are very welcome though :) |
@PragTob My issues with the README are as follows.
What do you mean? Which framework? This whole issue is about Minitest, if that's what you mean.
Rake does that. Note that the file is mentioned in 5.
Of course they are. At this point, I don't have a good overview over SimpleCov, though. I landed here googling how to get it working in my scenario, and I found the doc to be less than helpful; I had to cross-reference and handwave a lot of the commentary. Therefore, I left the above minimal example (with a reference on where to find it live and working) in the hope it might help the next googler. |
Locked just because this is old and long and isn't progressing any further. I admit it's not really resolved in the way I'd like. Please open a new issue and reference this one if you'd like to restart the 'simplecov' and 'minitest' don't always play well together conversation (both run in |
I am trying to use simplecov with minitests unit testing, I configured my test_helper.rb
But when i run the test cases, it shows me all the methods from models/controllers are not covered, for which there are test cases I have written. Only before filters, relations are seen green rest is all red.
Do i need some configuration? is Simplecov is supported for Minitest unit testing ?
The text was updated successfully, but these errors were encountered: