SimpleCov with Selenium/Rails - ruby-on-rails

We have a suite of Selenium tests. I'd like to use SimpleCov to coverage the server-side coverage of those tests. First off, is this a common approach? I haven't been able to find anything on SimpleCov/Selenium. Maybe SimpleCov is usually used for unit/functional tests instead of integration?
Current Selenium setup requires booting up a rails server, than having a suite of Selenium tests hit it. I'd need SimpleCov to run on the rails server, then quit after the suite is done.
Any help greatly appreciated!

simplecov author here. Whenever you launch SimpleCov, it applies the coverage analysis to the currently running process. Therefore, you would need to launch SimpleCov inside your Rails server process. I would recommend adding the SimpleCov setup as a conditional to your Rails app's config/boot.rb (at the very top), like so:
# config/boot.rb
if ENV["SELENIUM"]
require 'simplecov'
SimpleCov.start 'rails'
end
Before booting your Rails test server, set that environment variable. You should now receive a coverage report once the test server is shut down. Please check out the config options if you want to move it to another directory so it does not interfere with your regular (unit/functional) coverage report.
I am not sure that boot.rb is the right place though. The fact is that SimpleCov needs to be loaded before anything else in your app is required or it won't be able to track coverage for those files. You might need to experiment or have a look into the rails boot process to find that place, but as the Bundler setup is part of boot.rb (if I remember correctly...), putting the mentioned config above the Bundler.setup should be fine.
Basically, with a similar setup you can even get code coverage for your local manual browser-based testing by launching simplecov in your server process, clicking around and exiting the server, if for example you want to know what part of your application a certain action really touches.

Related

When running specs with RSpec in Rails, how do I access the test_database concurrently via the rails console or an external script?

With RSpec, I'm trying to test a Rails program that calls an external python script. When running the spec, RSpec has access to the test database rails_program_test. However, the python script cannot access the database rails_program_test.
Furthermore, while running rails console test in a separate terminal, queries show no records in the test database, even though the spec reports that records do exist. Finally, when I switch the RAILS_ENV to development, the python script has access to the rails_program_development database. Is there a way to access the test database outside of the spec?
This may be because the Rails app is using transactional fixtures. They are rolled back at the end of each test, so are never visible to external processes.
You might want to look at the DatabaseCleaner gem for a non-transactional approach, but your use case sounds very unusual.

Getting Capybara::DriverNotFoundError when trying to run Cucumber tests

I'm getting this error when I run the cucumber tests. Everything seemed to be working fine the previous day but I can't figure out why it stopped working. I was trying to get capybara webkit working and I had changed a couple of files but I don't see why it should affect my tests. Any idea on how to fix this error I'm getting while running the cucumber tests?
Capybara::DriverNotFoundError: no driver called :rack was found, available drivers: :rack_test, :selenium, :webkit, :webkit_debug
You mentioned that you edited many files. Could it be that you didn't revert all the changes you made? I think Capybara would pick the 'rack_test' driver by default, and your system could not find the 'rack' driver.
Since you're doing Cucumber testing, you must have a file called 'env.rb' under the features/support folder. Make sure you don't force 'rack' as your Capybara driver, and your tests should run fine.

Testing a Rails gem with test/dummy app, /tmp folder gets clobbered...somehow

I'm writing a gem that has to select which version of a XYZ.js file to make available to sprockets //= require "XYZ" statements based on configuration at app startup. My solution is to copy the XYZ.variant.js or XYZ.variant2.js to /tmp/cache/<gemname>/XYZ.js in the Rails app. This seems to work if I manually test; if I go to the test/dummy folder and test the functionality via rackup the XYZ.js is properly found. If I test the gem via another rails app, it works (via path: in Gemfile).
But, when I run the test suite for this gem, it fails, because at some point after the initial copy to /tmp/cache/<gemname>/XYZ.js, the whole tmp folder gets cleared, and the only thing in it by the time the test actually gets run is /tmp/cache/assets. I don't understand how this could be behaving different b/w the test suite and the other 2 working methods. It's as if the initialization order is different or something. Is there something special that running via rackup would do that would change the initialization order?
Note that the test suite worked fine before this particularly addition to the code that did the tmp copying. It's just the normal enginex code that would have been generated.
This has nothing to do with initializers, but rather another test case (for generators) that was clobbering the tmp folder.

Specjour and Spork Integration

I am using Specjour v0.4.1 for full test suite runs and preloading a rails app to run individual specs with Spork and Guard. Specjour uses multiple workers (usually 4) to run tests simultaneously.
How can I pass the preloaded application to each one of these workers? As specjour continues to load the application for each worker.
Note: rspec spec/models/example_spec.rb --drb is used to force rspec to use the preloaded application.
I've forked specjour and tried to push the --drb flag to each worker using several methods with no success. Any insight would appreciated. Thanks!
Note: hooks.rb configuration seems promising (mentioned in the README), however, documentation is extremely slim.

Why is RSpec so slow under Rails?

Whenever I run rspec tests for my Rails application it takes forever and a day of overhead before it actually starts running tests. Why is rspec so slow? Is there a way to speed up Rails' initial load or single out the part of my Rails app I need (e.g. ActiveRecord stuff only) so it doesn't load absolutely everything to run a few tests?
I definitely suggest checking out spork.
http://spork.rubyforge.org/
The railstutorial specifically addresses this, and gives a workaround to get spork running nicely in rails 3.0 (as of this moment, spork is not rails 3 ready out of the box). Of course, if you're not on rails 3.0, then you should be good to go.
The part of the tutorial showing how to get spork running in rails 3.0
http://railstutorial.org/chapters/static-pages#sec:spork
Checking when spork is rails 3.0 ready
http://www.railsplugins.org/plugins/440-spork
You should be able to to speed up your script/spec calls by running script/spec_server in a separate terminal window, then adding the additional -X parameter to your spec calls.
Why is rspec so slow? because it loads all the environement, loads fixtures and all that jazz.
Is there a way to speed up Rails' initial load you could try using mocks instead of relying on the database, this is actually correct for unit testing and will definitly speed up your unit tests. Additionnaly using the spec server as mentionned by #Scott Matthewman can help, same with the autotest from zentest mentionned by #Marc-Andre Lafortune
Is there a way to single out the part of my Rails app I need (e.g. ActiveRecord stuff only) so it doesn't load absolutely everything to run a few tests? what about this
rake test:recent
I am not sure how the rspec task integrate with this but you could definitely use the test:recent task as a template to do the same with rspec tests if the.
rake test:rspec:recent
doesn't exist yet
because it loads all the environement, loads fixtures and all that jazz.
The real culprit is if you run it using rake spec, it runs the db:test:prepare task.
This task drops your entire test database and re-creates it from scratch. This seems ridiculous to me, but that's what it does (the same thing happens when you run rake:test:units etc).
You can easily work around this using the spec application which rspec installs as part of the rspec gem.
Like this:
cd railsapp
spec spec # run all specs without rebuilding the whole damn database
spec spec/models # run model specs only
cd spec
spec controllers/user* # run specs for controllers that start with user
I think the "zen" experience you're looking for is to run spec_server and autospec in the background, with the result being near-instant tests when you save a file.
However, I'm having problems getting these two programs to communicate.
I found an explanation here:
I've noticed that autotest doesn't send commands to the spec_server.
Instead it reloads the entire Rails environment and your application's
plugins everytime it executes. This causes autotest to run
significantly slower than script server, because when you run the
script/spec command the specs are sent to the spec_server which
already has your Rails environment fired up and ready to go. If you
happen to install a new plugin or something like that, then you'll
have to restart the spec_server.
But, how do we fix this issue? I'm guessing it would involve downloading ZenTest and changing code for the autotest program, but don't have time to try it out right now.
Are you running this over Rails? If so, it's not RSpec's initialization that's slow, it's Rails'. Rails has to initialize the entire codebase and yours before running the specs. Well, it doesn't have to, but it does. RSpec runs pretty fast for me under my small non-rails projects.
Running tests can be really slow because the whole rails environment has to load (try script/console) and only then can all tests run. You should use autotest which keeps the environment loaded and will check which files you edit. When you edit and save a file, only the tests that depend on these will run automatically and quickly.
If you're using a Mac I recommend using Rspactor over autotest as it uses a lot fewer resources for polling changed files than autotest. There is both a full Cocoa version
RSpactor.app
or the gem version that I maintain at Github
sudo gem install pelle-rspactor
While these don't speed up individual rspec tests, they feel much faster as they auto run the affected spec's within a second of you hitting save.
As of rspec-rails-1.2.7, spec_server is deprecated in favor of the spork gem.
The main reason is that require takes forever on windows, for some reason.
Tips for speedup:
spork now works with windows, I believe.
You can try "faster_require" which caches locations:
http://github.com/rdp/faster_require
GL.
-rp
If you are on a Windows environment then there is probably little you can do as Rails seems to startup really slowly under Windows. I had the same experience on Windows and had to move my setup to a Linux VM to make it really zippy (I was also using autotest).

Resources