简体   繁体   English

如何使步骤定义文件中的步骤失败,以便在黄瓜水豚环境中将场景自动标记为失败?

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

Is there any simple way to mark the step as failed in cucumber so that scenario gets marked as failed in cucumber? 有没有简单的方法可以将步骤标记为在黄瓜中失败,从而使场景在黄瓜中被标记为失败?

My code in one of my step definition file which is written using Ruby language: 我的步骤定义文件之一中的代码是使用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

When I run my feature file, I get the this output Output_Snapshot 运行功能文件时,得到以下输出Output_Snapshot

I do not understand why do I get those lines in the output after line "Web server failed to start". 我不明白为什么在“ Web服务器无法启动”行之后在输出中得到这些行。

Is there any other simple way to fail the step in the step definition file? 还有其他简单方法可以使步骤定义文件中的步骤失败吗?

The extra lines are the stacktrace from the exception you raise. 多余的行是您引发的异常的堆栈跟踪。 You should be able to override the stacktrace if you specify an exception type. 如果指定异常类型,则应该能够覆盖stacktrace。 Also, this type of behavior is what the retry statement is for 另外,这种行为就是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