How to make Cucumber test continue running scenarios when RSpec ExpectationnotMet is thrown

会有一股神秘感。 提交于 2020-01-06 18:07:47

问题


I'm running scenarios with Cucumber/Ruby and every time the test runs, RSpec catches the error. All remaining steps (step definitions) are skipped. I don't want them to skip. I would like the program to run completely and only report that the issues happened. Any ideas on how I can get this working?

It looks kind of like this:

RSpec::Expectations::ExpectationNotMetError: expected "something" not to include "something else"

Skipped step
Skipped step
Skipped step
Skipped step
Skipped step
Skipped step
Skipped step
Skipped step
Skipped step
Skipped step


回答1:


Disclaimer: As Anthony pointed out, this is not best practice, but I'll assume you have a good reason for asking. :)

You need to build some form of error collector. Catch each RSpec exception and add that error to your collector. In an After hook, check to see if the collector contains something and fail the scenario if it does. You would also want to flesh out the error message collected to contain more information about which step failed and where it failed. This is just bare bones to give you an idea of what to do.

The key is to rescuing and logging your errors and then dealing with them later.


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

support/hooks.rb

Before do |scenario|
  @collected_errors = []
end

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

Output

  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


来源:https://stackoverflow.com/questions/26761624/how-to-make-cucumber-test-continue-running-scenarios-when-rspec-expectationnotme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!