即使失败也继续进行多主机测试

Continue multi-host tests even on failure

我已经构建了一些 serverspec 代码来 运行 在多个主机上进行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续到所有主机。

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

完整代码: https://gist.github.com/neilhwatson/1d41c696102c01bbb87a

此行为由 RSpec::Core::RakeTask#fail_on_error 控制,因此为了让它在所有主机上继续,您需要添加 t.fail_on_error = false。我也认为你不需要 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