Home

dhedlund edited this page Mar 17, 2013 · 6 revisions
Clone this wiki locally

Welcome to the spork-testunit wiki!

Install

gem install spork-testunit

Using

  • Don't bootstrap the test helper file for testunit. You don't need to, and it doesn't work with test unit.
  • In one console, run: spork testunit
    • ignore the bootstrap and other warnings
  • In another console, run the list of tests: testdrb test/unit/my_test_one.rb test/unit/my_test_two.rb
    • You can use: require 'test_helper' unless Spork.using_spork? to use Spork and no Spork.
  • In test helper:
require 'rubygems'
require 'spork'

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

Rails 3 & Test Unit

To correctly load the environment while running testdrb on your Rails tests make sure to add the test directory to the include path. A couple solutions:

  • Use the -I switch on the command line. Running a unit test from the Rails root we'd run testdrb -I test test/unit/mymodel_test.rb
  • Add the test/ directory to the $LOAD_PATH in your Spork.prefork block:

    # test/test_helper.rb
    
    Spork.prefork do 
      ENV["RAILS_ENV"] = "test"
      $LOAD_PATH << File.expand_path('..', __FILE__) # add test/ to include path
      # ...
    end

Default to Running All Tests and Directory Expansion

Unofficial: Instead of using testdrb test/**/*_test.rb to run all tests, you can monkey patch the gem in your to include any subset of test files if none are provided. This simplifies the command to just testdrb when you want to run all tests. The following code also allows allows for using directory names as arguments such as testdrb test/unit:

# test/test_helper.rb

Spork.prefork do
  class Spork::TestFramework::TestUnit
    alias_method :orig_run_tests, :run_tests
    def run_tests(argv, stderr, stdout)
      # run all tests in test/ if spork doesn't receive any filenames
      argv << 'test' if argv.all? {|arg| arg =~ /^-/ }

      # allow directories as arguments, auto-expand to test filenames
      argv = argv.map do |arg|
        File.directory?(arg) ? Dir["#{arg}/**/*_test.rb"] : arg
      end.flatten.uniq

      orig_run_tests(argv, stderr, stdout)
    end
  end

  # ...
end