From a5ca545d1e53829e383be3bd1a1da9e2eaf8b1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Mon, 20 Apr 2009 10:00:58 +0200 Subject: [PATCH] Pick up failure on after hook [#272 state:resolved] --- History.txt | 1 + Manifest.txt | 2 + examples/tickets/Rakefile | 6 +-- examples/tickets/features/272/hooks.feature | 26 ++++++++++ examples/tickets/features/272/hooks_steps.rb | 53 ++++++++++++++++++++ lib/cucumber/ast/outline_table.rb | 10 ++++ lib/cucumber/ast/scenario.rb | 15 ++++++ lib/cucumber/ast/step_collection.rb | 8 +++ 8 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 examples/tickets/features/272/hooks.feature create mode 100644 examples/tickets/features/272/hooks_steps.rb diff --git a/History.txt b/History.txt index 1c1bccfaa00..2a12162031e 100644 --- a/History.txt +++ b/History.txt @@ -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) diff --git a/Manifest.txt b/Manifest.txt index 0259ea1356c..d13b8568356 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -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_ diff --git a/examples/tickets/Rakefile b/examples/tickets/Rakefile index aa80bfeaa08..7adcda4f459 100644 --- a/examples/tickets/Rakefile +++ b/examples/tickets/Rakefile @@ -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] \ No newline at end of file diff --git a/examples/tickets/features/272/hooks.feature b/examples/tickets/features/272/hooks.feature new file mode 100644 index 00000000000..948109f4e7b --- /dev/null +++ b/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 + + Examples: + | something | + | I fail | + | I am undefined | + | I pass | diff --git a/examples/tickets/features/272/hooks_steps.rb b/examples/tickets/features/272/hooks_steps.rb new file mode 100644 index 00000000000..e89f25ecfc2 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/cucumber/ast/outline_table.rb b/lib/cucumber/ast/outline_table.rb index 2ecb55ba025..5de0d8caa67 100644 --- a/lib/cucumber/ast/outline_table.rb +++ b/lib/cucumber/ast/outline_table.rb @@ -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? diff --git a/lib/cucumber/ast/scenario.rb b/lib/cucumber/ast/scenario.rb index b3e3b359de1..287bbf0fa6a 100644 --- a/lib/cucumber/ast/scenario.rb +++ b/lib/cucumber/ast/scenario.rb @@ -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| diff --git a/lib/cucumber/ast/step_collection.rb b/lib/cucumber/ast/step_collection.rb index 145a672f8e7..a2050ae1009 100644 --- a/lib/cucumber/ast/step_collection.rb +++ b/lib/cucumber/ast/step_collection.rb @@ -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