Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Executing tests in a random order with rake

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

like image 380
Pietro Di Bello Avatar asked Sep 03 '09 22:09

Pietro Di Bello


1 Answers

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.

like image 80
Sam Saffron Avatar answered Nov 08 '22 13:11

Sam Saffron