ruby - RSpec - 如何拯救和重试测试?

标签 ruby rspec capybara

由于 Net::ReadTimeout 错误,我们的测试间歇性失败。

我们还没有找出永久性的解决办法。现在我们想尝试挽救该特定错误并重新运行测试。不是理想的解决方案或真正的解决方案,但我们需要在短期内有所作为。

我们如何重新运行失败的 rspec 测试?我的意思是测试套件如何为该测试自动执行此操作?

我们可以像这样捕获错误:

# spec/support/capybara.rb
def rescue_net_read_timeout(max_tries = 1, tries = 0, &block)
  yield
rescue Net::ReadTimeout => e
  Rails.logger.error e.message
end

但是我们如何让它尝试重新运行该测试? 我们想尝试重新运行测试,然后如果重新运行通过且没有错误(理想情况下记录它),否则失败并考虑测试,因此套件失败。

最佳答案

以下方法使用 wait gem重试测试 - 或测试的一部分,可能只是一个断言。 它

def retry_test(&block)
  Wait.new(delay: 0.5, attempts: 10).until do
    begin
      yield    
      true # test passed
    rescue RSpec::Expectations::ExpectationNotMetError => x
      # make sure it failed for the expected reason
      expect(x.to_s).to match /Net::ReadTimeout/
      false # test failed, will retry
    end
  end
end

可以验证失败的原因,如果测试因其他原因失败,则立即报告失败。

it 'should get a response' do
  retry_test do
    # The entire test, or just the portion to retry
  end
end

关于ruby - RSpec - 如何拯救和重试测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125479/

相关文章:

ruby - 通过 Capistrano 创建 crontab 而不是使用 crontab -e

ruby-on-rails - 使用工厂女孩和 capybara 将日期输入到表单中

rspec - Capybara/RSpec 'have_css' 匹配器不起作用,但 has_css 起作用

ruby - 单击不使用 React 组件 + capypara

ruby-on-rails - 在我的 RSpec 请求规范中调用 sleep 是否有效?

当存在指向目录 : alternatives? 的符号链接(symbolic link)时,Ruby FileUtils.rm_r 在 Windows 上失败

ruby-on-rails - Ruby on Rails - 不选择给定字段

ruby-on-rails - 如何忽略 Rails 中的路线?

javascript - Rspec & capybara : Setting focus to an element using jQuery doesn't apply `:focus` CSS

css - save_and_open_page 已停止提供我的 CSS