簡體   English   中英

如何使步驟定義文件中的步驟失敗,以便在黃瓜水豚環境中將場景自動標記為失敗?

[英]How to fail a step in step definition file so that scenario is automatically marked as failed in cucumber-capybara environment?

有沒有簡單的方法可以將步驟標記為在黃瓜中失敗,從而使場景在黃瓜中被標記為失敗?

我的步驟定義文件之一中的代碼是使用Ruby語言編寫的:

SECONDS_TO_SLEEP = 5
MAX_NUM_ITERATIONS = 3

Given(/^The TREC UI is running$/) do
    elapsedTime = 1
    currentAttempts = 0
    while currentAttempts <= MAX_NUM_ITERATIONS
        begin
            visit "http://sut:8080/myPage"
            expect(page).to have_content("Welcome to My Page")
            totalTime = elapsedTime + currentAttempts * SECONDS_TO_SLEEP
            puts "It took #{totalTime} seconds for TREC UI to come up."
            break

        rescue
            currentAttempts += 1
            sleep SECONDS_TO_SLEEP
            if currentAttempts <= MAX_NUM_ITERATIONS
                puts "Waiting for web server to start."
            else
                raise "Web server failed to start."
            end
        end
    end
end

運行功能文件時,得到以下輸出Output_Snapshot

我不明白為什么在“ Web服務器無法啟動”行之后在輸出中得到這些行。

還有其他簡單方法可以使步驟定義文件中的步驟失敗嗎?

多余的行是您引發的異常的堆棧跟蹤。 如果指定異常類型,則應該能夠覆蓋stacktrace。 另外,這種行為就是retry語句的目的

Given(/^The TREC UI is running$/) do
  elapsedTime = 1
  currentAttempts = 0
  begin
    visit "http://sut:8080/myPage"
    puts "Waiting for web server to start."
    expect(page).to have_content("Welcome to My Page", wait: SECONDS_TO_SLEEP)
    totalTime = elapsedTime + currentAttempts * SECONDS_TO_SLEEP # This needs some math fixup if using the wait option above - won't work with rack_test driver though
    puts "It took #{totalTime} seconds for TREC UI to come up."
  rescue
    retry if (currentAttempts += 1) <= MAX_NUM_ITERATIONS
    # If using the rack_test driver then the :wait option above won't
    # work so you would need to use sleep like below instead of the line above
    # if (currentAttempts += 1) <= MAX_NUM_ITERATIONS
    #   sleep SECONDS_TO_SLEEP
    #   retry
    # end
    raise RuntimeError, "Web server failed to start.", []  # The [] overrides the stack info with an empty array
  end
end 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM