由于 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/