Speed up Rake Cucumber test run in Rails 4 - ruby-on-rails

I'd like to use Cucumber via Rake for BDD in a Rails 4. I've got it installed and hooked into spring to speed up tests but they take much longer than expected to run. The tests definitively run faster when Spring is active, but there is always ~2 seconds of startup time for Cucumber even if there are no feature/scenarios to run. For example:
$ bin/spring status
Spring is not running.
$ time bin/rake cucumber
/Users/alans/.rvm/rubies/ruby-2.1.1/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
0 scenarios
0 steps
0m0.000s
bin/rake cucumber 0.08s user 0.02s system 3% cpu 3.463 total
$ bin/spring status
Spring is running:
64383 spring server | cucumber_test | started 48 secs ago
64384 spring app | cucumber_test | started 48 secs ago | test mode
$ time bin/rake cucumber
/Users/alans/.rvm/rubies/ruby-2.1.1/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
0 scenarios
0 steps
0m0.000s
bin/rake cucumber 0.08s user 0.02s system 4% cpu 2.098 total
Using bin/rake for the run is what's defined in the Spring Readme. Running spring without rake produces the faster expected times.
$ time spring cucumber
Using the default profile...
0 scenarios
0 steps
0m0.000s
spring cucumber 0.06s user 0.01s system 13% cpu 0.537 total
The Question
How can I setup Cucumber, Spring and Rake so that tests run as quickly with Rake as without?
Further Details
My environment is an RVM install of ruby-2.1.1 and rails 4.1.1 on a Mac with an SSD running 10.9.3.
Here's what I'm doing:
Create an app with
rails new cucumber_test -T
Update the gem file with the following:
group :development, :test do
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'rspec-rails'
gem 'spring'
gem 'spring-commands-cucumber'
end
Then running:
bundle install
(I also tried leaving the default listing for spring in just the :development environment but that didn't speed things up either.)
Run the Cucumber generator:
rails g cucumber:install
Update the Spring binstubs:
bundle exec spring binstub --all
Which returns:
* bin/rake: spring already present
* bin/cucumber: generated with spring
* bin/rails: spring already present
This is where I run the time tests. When Spring is stopped, Cucumber consistently takes longer to run. Even with Spring running it seems there is always a 2 second overhead before the test suite actually kicks off when using Rake. That's what I'm trying to eliminate.

Related

Rails 7 - Difference between "Rails test" and "bundle exec rake test"

I'm starting a new Rails 7 app and have about 150 tests on Minitest.
I've seen in some places people using in the terminal of command rails test and other places recommending bundle exec rake test
I have some basic tests and when I run both commands in my terminal, the same exact things appear and I have the same amount of tests with the same test results.
So I'm wondering if there is any difference between the two on Rails 7?
The difference between those commands is from where rails is called.
Running commands with bundler allows you to execute them without having the packages installed in your system.
For example, if you don't have rails installed globally you couldn't run commands using rails directly
rails test will raise the following error:
Rails is not currently installed on this system. To get the latest
version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
But if you run bundle exec rails test instead you should run your test suite perfectly because you're calling rails through bundle.
In your case using bundle exec rake test will ensure that you're using the same version of your Gemfile.lock.

"spring rake" runs specs regardless of what rake task is specified

If I use spring rake, it runs my project specs to matter what rake task is specified. It just completely ignores whatever is specified at the command line and runs the specs. Here's an example:
$ spring rake routes
/Users/rsilva/.rbenv/versions/2.0.0-p353/bin/ruby -I/Users/rsilva/.rbenv/versions/2.0.0- p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.2/lib:/Users/rsilva/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-support-3.1.0/lib /Users/rsilva/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rspec-core-3.1.2/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
Run options: exclude {:expensive=>true, :migration=>true}
# then runs all the project specs
If I don't use spring, it works fine:
$ bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
# then prints out all the routes
I'm using Spring outside of my Gemfile (I did gem install spring).
Here's the versions of spring and spring-commands-rspec I'm using:
$ gem list --local | grep -P 'spring'
spring (1.1.3)
spring-commands-rspec (1.0.2)
Here's my ~/.spring.rb:
$ cat ~/.spring.rb
require 'spring-commands-rspec'
How do I get spring rake to execute the task specified?
Looks like rake is just executing the default task regardless of arguments passed. This seems to be an issue with rake 10.4.0 and spring. Try upgrading rake to 10.4.1.
https://github.com/rails/spring/issues/366

spring using by default

I upgraded rails to 4.1.0 & installed gem "spring"
After it, I patched bundle exec spring binstub --all. Now, all my bin/* includes
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
But, if I run, saying time spring rake -T & time rake -T times 1 second & 3 seconds! It seems that spring not used by default in my application. What's going wrong? It's bad thing that I need to write annoying spring ..command.. before any rake task, rails server or rspec. What I need to do to avoid spring command with spring worked by default?
You don't need to prepend spring everytime. I'm not sure why you're getting those numbers but try the following.
# make sure that spring server is stopped
>> spring stop
# take note of the time here
>> time bin/rake routes
# start spring server
>> spring
# test routes again
>> time bin/rake routes
The second bin/rake routes should be very fast. In one of our apps, without spring, it takes 8 seconds to show the routes. With spring, it's less than a second.
For rspec, you need to install an additional gem for that. Look at the additional commands section of the spring gem

`rspec` with `spring` doesn't work

I wanted to use spring instead of spork.
I installed spring and spring-commands-rspec, and after installing I created binstubs by bundle exec spring binstub --all.
When I run bin/rspec, I get only this result.
No examples found.
Finished in 0.00099 seconds
bin/rake routes gives me proper result. And bundle exec rspec tests all example I have like this.
Finished in 6.63 seconds
20 examples, 2 failures, 11 pending
Failed examples:
What am I missing? Where should I configure the place of test directories?
Try specifiying the directory:
$ bin/rspec spec/

`spring rspec` just displays spring help.. why?

I'm trying to use spring with rspec and guard on a Rails 3.2.16 project.
After installing spring and spring-commands-rspec, I created the binstubs with :
> bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rspec: spring inserted
* bin/rails: spring inserted
Now trying to run spec with spring fails (gems paths and project path subtituted for legibility) :
> bin/rspec spec/
Version: 1.1.0
Usage: spring COMMAND [ARGS]
Commands for spring itself:
binstub Generate spring based binstubs. Use --all to generate a binstub for all known commands.
help Print available commands.
status Show current status.
stop Stop all spring processes for this project.
Commands for your application:
rails Run a rails command. The following sub commands will use spring: console, runner, generate, destroy.
rake Runs the rake command
rspec Runs the rspec command
rspec binstub
No DRb server is running. Running in local process instead ...
gemspath/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load': cannot load such file -- [PROJECT_PATH]/rspec (LoadError)
from gemspath/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from gemspath/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from gemspath/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from gemspath/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from gemspath/rspec-core-2.14.7/lib/rspec/core/runner.rb:77:in `rescue in run'
from gemspath/rspec-core-2.14.7/lib/rspec/core/runner.rb:73:in `run'
from gemspath/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
Other try :
> spring rspec
Only displays spring help
Same goes for
> spring rspec spec/
Here is the rspec binstub (bin/rspec) :
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require 'bundler/setup'
load Gem.bin_path('rspec', 'rspec')
Any idea ?
I had the same problem. I've solved it by stopping spring and adding to my Gemfile:
gem 'spring-commands-rspec', group: :development, require: false
Spring needs to be restarted in order to use the newly added command.
bin/spring stop
bin/rspec path/to/file.rb
ANSWER OUTDATED! Was fixed. See #pfleidi's answer below.
I've solved the problem, even if i dont know why it has solved the problem.
Just remove gem 'spring', group: :development from the Gemfile so that you only have gem 'spring-commands-rspec' left.
Run bundle install and then spring rpsec spec will work! Its sooo fast :)
You have to restart the spring. So stop it and then re-run it
spring stop
spring rspec
Then it should works as expected.

Resources