What have you found for these years?

2011-10-18

Run tests faster and iterate faster in Rails 3.1!

So here's the story. I was mumbling and complaining
Rails on Twitter, saying:

running tests in rails 3.1 is insane slow. our tests cases run in
2 seconds, while loading rails to run them requires 45 seconds

And thanks Sigfrid for letting me know about Spork.
It really saves a huge amount of time for me, and it's
still counting! For every time I need to fix some test
failures on this slow-slow Rails 3.1, I feel appreciated.
I really only need 2 to 3 seconds to run the whole tests now.

Although it didn't work out-of-the-box for me. I spent
some time fixing some issues, looking at the source,
playing around with DRb, and so on so forth.

Here's my requirement.

First of all, the normal `rake test` command should still work.
This is important, because we're on team works, introducing
incompatibility is bad. And sometimes I just want to run the tests.
If you follow the instruction on Spork to setup it, you would
always need to run the server in order to run the tests.
This is very bad, needless incompatibility, in my opinion.

So I looked at the source, Spork is looking for this "words"
Spork.prefork to detect if Spork has been installed
or not. So I inserted this line in test/test_helper.rb to trick Spork:
# Spork.prefork
This way, we can make normal `rake test` work but still make
Spork thinks it's installed (bootstrapped, in their term).

But this hack would also make the normal `spork` command
ineffective, a lot of Spork trick won't work under this setup.
This means, I need to have another way to run Spork.

So I'd written two scripts to handle this. I put them in bin/spork
and bin/test. In order to take the advantage of spork, first run
`./bin/spork`, then open another terminal tab, run `./bin/test`.

Here's the gist to demonstrate this:
Run Spork without affecting `rake test`
Also remember to put this # Spork.prefork
in test/test_helper.rb to trick Spork!



Well, personally I think this should be merged into Spork!
But I don't know, I just want to make it work for me at
the moment, I didn't write it too carefully and didn't
consider if this won't break the normal Spork.

If anyone interested, we can see how we could make this
merged into Spork. Please let me know if you're interested,
thanks!


--
Extracted form gist:
bin/spork
#!/usr/bin/env ruby

$LOAD_PATH.unshift('./lib').unshift('./test')

require 'spork'

# monkey patch...
class Spork::Server
  def run_with str
    eval(str)
  end
end

load Gem.bin_path('spork', 'spork')

bin/test
#!/usr/bin/env ruby

require 'drb'
# this allows Ruby to do some magical stuff so you can pass an output stream over DRb.
DRb.start_service("druby://127.0.0.1:0")

obj = DRbObject.new_with_uri("druby://127.0.0.1:8988")

obj.run_with <<-RUBY
require './config/application'
::Rails.application.require_environment!
RUBY

obj.run_with <<-RUBY
# This code will be run each time you run your specs.
loadup = lambda{ |glob|
Dir[glob].each{ |f| load f }
}
loadup['app/**/*.rb']
loadup['lib/**/*.rb']
RUBY

obj.run(Dir['test/**/*_test.rb'], $stderr, $stdout)

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0