Andrew Allison
Andrew Allison

Reputation: 1136

Cucumber: Is it possible to manually set the scenario status in the 'After' hook?

I am aware that this is a needless use case and a very bad practice. However, I am still curious to see if it would even be possible to manually set the status of a scenario from within the 'After' hook. I am using Cucumber 2.4.0 with Ruby for my Test Automation.

Upvotes: 1

Views: 1432

Answers (1)

Andrew Allison
Andrew Allison

Reputation: 1136

After some research I found out that it is possible to manually set the status of a scenario in the 'After' hook. See the example below

After do |scenario|
  duration = scenario.instance_variable_get(:@result).instance_variable_get(:@duration)
  if scenario.failed?
    # Manually set to passed
    passed = Cucumber::Core::Test::Result::Passed.new(duration)
    scenario.instance_variable_set(:@result, passed)
  elsif scenario.passed?
    # Manually set to failed
    exception = StandardError.new("message")
    failed = Cucumber::Core::Test::Result::Failed.new(duration, exception)
    scenario.instance_variable_set(:@result, failed)
  end

Feel free to add suggestions/comments

Upvotes: 1

Related Questions