Skip to content

Commit

Permalink
Pick up failure on after hook [#272 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Apr 20, 2009
1 parent 50957da commit a5ca545
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 3 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -2,6 +2,7 @@

=== New features
* New translation for Hungarian (#287 Bence Golda)
* Pick up failure on after hook (#272 Aslak Hellesøy)

=== Bugfixes
* Exceptions from steps called within hooks are now reraised. (#294 Ben Mabey)
Expand Down
2 changes: 2 additions & 0 deletions Manifest.txt
Expand Up @@ -195,6 +195,8 @@ examples/tickets/features/246.feature
examples/tickets/features/248.feature
examples/tickets/features/270/back.feature
examples/tickets/features/270/back.steps.rb
examples/tickets/features/272/hooks.feature
examples/tickets/features/272/hooks_steps.rb
examples/tickets/features/279/py_string_indent.feature
examples/tickets/features/279/py_string_indent.steps.rb
examples/tickets/features/279/wrong.feature_
Expand Down
6 changes: 3 additions & 3 deletions examples/tickets/Rakefile
Expand Up @@ -2,15 +2,15 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib')
require 'cucumber/rake/task'

Cucumber::Rake::Task.new(:pretty) do |t|
t.cucumber_opts = "--format pretty -q"
t.cucumber_opts = "--tags ~@intentional_failure --format pretty -q"
end

Cucumber::Rake::Task.new(:html) do |t|
t.cucumber_opts = "--format html --out features.html"
t.cucumber_opts = "--tags ~@intentional_failure --format html --out features.html"
end

Cucumber::Rake::Task.new(:progress) do |t|
t.cucumber_opts = "--format progress -i -n"
t.cucumber_opts = "--tags ~@intentional_failure --format progress -i -n"
end

task :default => [:pretty, :html, :progress]
26 changes: 26 additions & 0 deletions examples/tickets/features/272/hooks.feature
@@ -0,0 +1,26 @@
@intentional_failure
Feature: Hooks
In order to integrate with my complex environment
I need to check scenario status in an After block

@272_failed
Scenario: Failed
Given I fail

@272_undefined
Scenario: Undefined
Given I am undefined

@272_passed
Scenario: Passed
Given I pass

@272_outline
Scenario Outline: Should work too
Given <something>

Examples:
| something |
| I fail |
| I am undefined |
| I pass |
53 changes: 53 additions & 0 deletions examples/tickets/features/272/hooks_steps.rb
@@ -0,0 +1,53 @@
require 'spec/expectations'

Given /^I fail$/ do
raise "BOOM (this is expected)"
end

Given /^I pass$/ do
end

module HookChecks
def check_failed(scenario)
scenario.should be_failed
scenario.should_not be_passed
scenario.exception.message.should == "BOOM (this is expected)"
end

def check_undefined(scenario)
scenario.should_not be_failed
scenario.should_not be_passed
end

def check_passed(scenario)
scenario.should_not be_failed
scenario.should be_passed
end
end

World(HookChecks)

After('@272_failed') do |scenario|
check_failed(scenario)
end

After('@272_undefined') do |scenario|
check_undefined(scenario)
end

After('@272_passed') do |scenario|
check_passed(scenario)
end

counter = 0
After('@272_outline') do |scenario|
case(counter)
when 0
check_failed(scenario)
when 1
check_undefined(scenario)
when 2
check_passed(scenario)
end
counter +=1
end
10 changes: 10 additions & 0 deletions lib/cucumber/ast/outline_table.rb
Expand Up @@ -70,6 +70,16 @@ def accept_hook?(hook)
@table.accept_hook?(hook)
end

# Returns true if one or more steps failed
def failed?
@step_invocations.failed?
end

# Returns true if all steps passed
def passed?
@step_invocations.passed?
end

private

def header?
Expand Down
15 changes: 15 additions & 0 deletions lib/cucumber/ast/scenario.rb
Expand Up @@ -34,6 +34,21 @@ def accept(visitor)
end
end

# Returns true if one or more steps failed
def failed?
@steps.failed?
end

# Returns true if all steps passed
def passed?
@steps.passed?
end

# Returns the first exception (if any)
def exception
@steps.exception
end

def skip_invoke!
@steps.each{|step_invocation| step_invocation.skip_invoke!}
@feature.next_feature_element(self) do |next_one|
Expand Down
8 changes: 8 additions & 0 deletions lib/cucumber/ast/step_collection.rb
Expand Up @@ -54,6 +54,14 @@ def exception
@exception ||= ((failed = @steps.detect {|step| step.exception}) && failed.exception)
end

def failed?
@steps.detect{|step_invocation| step_invocation.status == :failed}
end

def passed?
@steps.detect{|step_invocation| step_invocation.status != :passed}.nil?
end

def to_sexp
@steps.map{|step| step.to_sexp}
end
Expand Down

0 comments on commit a5ca545

Please sign in to comment.