LoginSignup
1
1

More than 5 years have passed since last update.

Spork-testunit - speed up testing of Rails

Last updated at Posted at 2012-03-16

Testing with Rails3.1 is quite slow.So I tried Spork to speed it up.
Spork is a kind of server for testing. Using that,we can skip initialization of rails app which is too heavy for each testing.
While Spork is aimed to run with RSpec,I'm using test:unit (it's rails' default,as you know). So I put Spork and Spork-testunit gems in Gemfile.

Gemfile
group :test, :development do
  gem 'spork'
  gem 'spork-testunit'
end

then,as usual

$ bundle install

after that,we have to prepare test_helper.rb for Spork.First,execute the command like this

$ spork testunit --bootstrap

Now,in test/test_helpr.rb,you can find Spork.prefork and Spork.each_run block.
So I put some code in those block as follows

test_helper.rb
Spork.prefork do
  ENV["RAILS_ENV"] = "test"
  require File.expand_path('../../config/environment', __FILE__)
  require 'rails/test_help'

  class ActiveSupport::TestCase
    fixtures :all
    def login(user_id=nil)   
      @request.session[:user_id] = user_id
    end
  end
end

Spork.each_run do
  load "#{Rails.root}/config/routes.rb" 
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end

Weird thing is that I have to load all the files in each_run block explicitly. However,without that,Spork server couldn't get any changes I've made on models or controllers. It could be bug,no?

Then start Spork server

$ spork

To do actual testing,do like this

$ testdrb -Itest test/units/*rb

Testing will be executed with Spork server.
In my case,it took less than half the time of simple "rake test" testing. Worth trying.

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1