How can I have the tests for my Rails app to be executed in a random order? Is there a simple solution using rake?
Asked
Active
Viewed 1,395 times
8
-
3Why would you want to do that? – Sam Saffron Sep 03 '09 at 22:22
-
3To ensure that there are no dependencies between tests? – Andrew Grimm Sep 04 '09 at 01:08
-
1Yes, the main point for us is to ensure that there are no dependencies between tests. – Pietro Di Bello Sep 04 '09 at 07:31
-
See also [Why does Test::Unit.test_order= not working as expected?](http://stackoverflow.com/questions/11185517/why-does-testunit-test-order-not-working-as-expected) – knut Dec 13 '12 at 08:35
2 Answers
5
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
- 128,308
- 78
- 326
- 506
-
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 - 114 RubyGems: 1.3.5 Rake: 0.8.7 Rails: 2.3.3 Do you have more ideas? Tx! – Pietro Di Bello Sep 04 '09 at 10:19
0
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
- 78,473
- 57
- 200
- 338
-
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. – Pietro Di Bello Sep 04 '09 at 12:13