2017-02-21 72 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 
相关问题