5

I've built some serverspec code to run a group of tests on multiple hosts. The trouble is that the testing stops at the current host when any test fails. I want it to continue to all hosts even if a test fails.

The Rakefile:

namespace :spec do
  task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
  hosts.each do |host|
    begin
      desc "Run serverspec to #{host}"
      RSpec::Core::RakeTask.new(host) do |t|
        ENV['TARGET_HOST'] = host
        t.pattern = "spec/cfengine3/*_spec.rb"
      end
    rescue
    end
  end
end

Complete code: https://gist.github.com/neilhwatson/1d41c696102c01bbb87a

wurde
  • 2,487
  • 2
  • 20
  • 39
Neil H Watson
  • 1,002
  • 1
  • 14
  • 31

1 Answers1

8

This behaviour is controlled by RSpec::Core::RakeTask#fail_on_error so in order to have it continue on all hosts you need to add t.fail_on_error = false. I also think that you don't need to rescue.

namespace :spec do
  task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
  hosts.each do |host|
    desc "Run serverspec to #{host}"
    RSpec::Core::RakeTask.new(host) do |t|
      ENV['TARGET_HOST'] = host
      t.pattern = "spec/cfengine3/*_spec.rb"
      t.fail_on_error = false
    end
  end
end
egwspiti
  • 957
  • 5
  • 10
  • 1
    Setting `t.fail_on_error = false` also causes rake to always return an exit code of 0 to the calling process. Unfortunately there doesn't seem to be a `continue_on_error` or similar method to ensure that all tasks run, and failures are still reported on finish. – conorsch Jul 24 '15 at 21:14