If one of the Rake tasks that the :test (default) task depends on fails, you want to be alerted of that as it helps you find the root of failure (the first error/exception is usually what you want to look at). Currently rake :test will swallow exceptions thrown for example by a task such as db:test:prepare which makes debugging very hard in certain cases.
This simple patch only affects the :test task and makes sure you get the stack trace to help debugging whilst retaining the behaviour of the current code to not abort testing but running all three of test:units, test:functionals, and test:integration even if an exception is thrown. The new :test task should be functionally equivalent to the old one with the only difference being that exceptions are now reported when they occur.
The new :test task after the patch should be:
desc 'Test all units and functionals'
task :test do
test_tasks = ["test:units", "test:functionals"]
test_tasks << "test:integration" if File.exist?("test/integration")
test_tasks.each do |test_task|
begin
Rake::Task[test_task].invoke
rescue => e
got_error = true
puts("Rake task '#{test_task} threw exception #{e} - #{e.backtrace.join("\n ")}")
end
end
raise "Test failures" if got_error
end
See ticket 3709: http://dev.rubyonrails.org/ticket/3709
The patch is:
Index: railties/lib/tasks/testing.rake
===================================================================
--- railties/lib/tasks/testing.rake (revision 5468)
+++ railties/lib/tasks/testing.rake (working copy)
@@ -40,11 +40,16 @@
desc 'Test all units and functionals'
task :test do
- Rake::Task["test:units"].invoke rescue got_error = true
- Rake::Task["test:functionals"].invoke rescue got_error = true
-
- if File.exist?("test/integration")
- Rake::Task["test:integration"].invoke rescue got_error = true
+ test_tasks = ["test:units", "test:functionals"]
+ test_tasks << "test:integration" if File.exist?("test/integration")
+
+ test_tasks.each do |test_task|
+ begin
+ Rake::Task[test_task].invoke
+ rescue => e
+ got_error = true
+ puts("Rake task '#{test_task} threw exception #{e} - #{e.backtrace.join("\n ")}")
+ end
end
raise "Test failures" if got_error