【问题标题】:How to make Cucumber test continue running scenarios when RSpec ExpectationnotMet is thrown抛出 RSpec ExpectationnotMet 时如何使 Cucumber 测试继续运行场景
【发布时间】:2017-01-28 01:41:56
【问题描述】:

我正在使用 Cucumber/Ruby 运行场景,每次运行测试时,RSpec 都会捕获错误。跳过所有剩余的步骤(步骤定义)。我不想让他们跳过。我希望程序完全运行并且只报告问题发生了。关于如何让这个工作的任何想法?

看起来像这样:

RSpec::Expectations::ExpectationNotMetError:预期的“某物”不包括“其他东西”

跳过的步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤
跳过步骤

【问题讨论】:

  • 这将违背 TDD - 它总是会在它为该测试命中的第一个异常时爆炸。

标签: ruby cucumber


【解决方案1】:

免责声明:正如 Anthony 指出的那样,这不是最佳做法,但我认为您有充分的理由提出要求。 :)

您需要构建某种形式的错误收集器。捕获每个 RSpec 异常并将该错误添加到您的收集器。在 After 挂钩中,检查收集器是否包含某些内容,如果包含则使场景失败。您还希望充实收集到的错误消息,以包含有关哪个步骤失败以及在何处失败的更多信息。这只是简单的介绍,可以让您了解该怎么做。

关键是拯救和记录你的错误,然后再处理它们。


test.feature

  Scenario: Test out someting
    Given this step passes
    And this step has a collected error
    Then this step passes

test_stepdef.rb

Given(/^this step passes$/) do
  # keep on keeping on
end

Given(/^this step has a collected error$/) do
  begin
    1.should eq(0)
  rescue RSpec::Expectations::ExpectationNotMetError => e
    @collected_errors.push e.message
  end

end

支持/hooks.rb

Before do |scenario|
  @collected_errors = []
end

After do |scenario|
  fail "#{@collected_errors}" unless @collected_errors.empty?
end

输出

  Scenario: Test out someting           # features/test.feature:6
    Given this step passes              # features/stepdefs/test_stepdef.rb:2
    And this step has a collected error # features/stepdefs/test_stepdef.rb:6
    Then this step passes               # features/stepdefs/test_stepdef.rb:2
      ["expected: 0
          got: 1
          (compared using ==)"] (RuntimeError)
      features/support/hooks.rb:21:in `After'

Failing Scenarios:
cucumber features/test.feature:6 # Scenario: Test out someting

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 2019-04-16
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2020-01-03
    • 2016-06-20
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多