Ruby - Executing tests in a random order with rake

2019-04-24 02:05发布

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

2条回答
beautiful°
2楼-- · 2019-04-24 02:31

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.

查看更多
Emotional °昔
3楼-- · 2019-04-24 02:31

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!
查看更多
登录 后发表回答