views:

154

answers:

2

How can I have the tests for my Rails app to be executed in a random order? Is there a simple solution using rake?

A: 

You may wish to check out "ZenTest 3.9.0: now with more Evil" (can't do a direct link, use google's cache)

Added ability to set test execution order, defaults to :random. EVIL!
Andrew Grimm
I played some time with ZenTest, but I never guess how to enable random order. I mean, the default execution order is fixed and remains the same across different test executions.
pieroz
+2  A: 

Here you go, define this in lib/tasks/tasks.rb

namespace :test do 
  namespace :randomize do 
    desc "Randomize tests"
    Rake::TestTask.new(:all => "db:test:prepare") do |t|
      t.libs << "test"
      t.test_files = Rake::FileList[
        'test/unit/**/*_test.rb',
        'test/functional/**/*_test.rb', 
        'test/integration/**/*_test.rb' 
      ].shuffle
      t.verbose = true
    end
  end
end

Run: rake test:randomize:all

Keep in mind that within file tests will still be executed in the order they appear. I guess you could monkey patch test unit to allow for that.

Sam Saffron
Thanks Sam.This does not work to me, and actually the solution you posted is the same one I came to: simply shuffle the array containing the list of test files to execute.As a matter of fact, the order of test files you pass to the rake TestTask seems to be ignored, meaning that internally rake will sort this list alfabetically on the name of the file.I verify this behavior executing the rake task with the TESTOPTS="-v" option, to monitor the actual test execution order.Here are the versions I use:Ruby: 1.8.6 - 114RubyGems: 1.3.5Rake: 0.8.7Rails: 2.3.3Do you have more ideas? Tx!
pieroz