[英]RSpec - how to rescue and retry the test?
由於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
但是我們如何讓它嘗試重新運行該測試? 我們想嘗試重新運行測試,然后如果重新運行通過沒有錯誤繼續前進(盡管最好記錄它),否則真正失敗並考慮測試,因此套件失敗。
我使用名為rspec-repeat的 ruby gem。 在幾百個自動化測試中,我們可能會在這里或那里遇到一個不穩定的測試,這有助於我們克服誤報。
理想情況下,最好繼續診斷這些不穩定的測試,但這有助於在此期間緩解問題。
旁注, rspec-repeat
基於另一個名為rspec-retry
庫,但我發現這個庫的代碼庫更整潔,配置更易於使用。
以下方法使用等待 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.