2017-02-21 60 views
0

是否有任何簡單的方法在黃瓜上將步驟標記爲失敗,以便在黃瓜中將場景標記爲失敗?如何使步驟定義文件中的步驟失敗,以便在黃瓜 - 水豚環境中自動將情景標記爲失敗?

我在使用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服務器無法啓動」行之後,是否在輸出中顯示這些行?

有沒有其他簡單的方法來失敗步驟定義文件中的步驟?

回答

2

額外的行是來自您引發異常的堆棧跟蹤。如果指定了異常類型,您應該能夠覆蓋堆棧跟蹤。此外,這種類型的行爲是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