How to make sure cucumber stops execution when a step fails?

6,042 views
Skip to first unread message

planon

unread,
Jul 18, 2011, 7:37:29 PM7/18/11
to Cukes
Cucumber keeps executing even after a step definition has failed, and
it outputs not only the failing steps, but also the passing ones. What
option do I need to use in order to ensure that Cucumber stop
executing as soon as it has encountered a failing step?

This is my cucumber.yml file

<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format
#{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format
#{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --
tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --
tags ~@wip
autotest: <%= std_opts %> features --color

Matt Wynne

unread,
Jul 19, 2011, 5:17:31 AM7/19/11
to cu...@googlegroups.com

You can use an After hook:

After do |scenario|
exit 1 if scenario.failed?
end

cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/books/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne

Dmitriy Korobskiy

unread,
Aug 1, 2011, 6:43:02 PM8/1/11
to cu...@googlegroups.com
On 7/19/11 5:17 AM, Matt Wynne wrote:
On 19 Jul 2011, at 00:37, planon wrote:

Cucumber keeps executing even after a step definition has failed, and
it outputs not only the failing steps, but also the passing ones. What
option do I need to use in order to ensure that Cucumber stop
executing as soon as it has encountered a failing step?

[skipped]
You can use an After hook:

    After do |scenario|
      exit 1 if scenario.failed?
    end

cheers,
Matt
This did not work for me. What happens is that I see the error stack trace with exit (SystemExit) in it, but Cucumber (1.0.2) continues as usual.

Here is how stack trace looks like:

    And the deleted cart should be removed from the list of My Open Carts         # features/step_definitions/RAP_steps.rb:195
      Unable to locate a cell at index 6 (Watir::Exception::UnknownCellException)
      ./features/support/env.rb:68:in `[]'
      ./features/support/env.rb:57:in `block in strings'
      ./features/support/env.rb:56:in `each'
      ./features/support/env.rb:56:in `strings'
      ./features/step_definitions/RAP_steps.rb:202:in `/^the\ deleted\ cart\ should\ be\ removed\ from\ the\ list\ of\ My\ Open\
 Carts$/'
      features\cart_actions.feature:51:in `And the deleted cart should be removed from the list of My Open Carts'
      exit (SystemExit)
      M:/Users/DK/Projects/RAP/WorkingCopy/features/support/env.rb:165:in `exit'
      M:/Users/DK/Projects/RAP/WorkingCopy/features/support/env.rb:165:in `After'

The hook looks like this:

# run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps.
After do |scenario|
  browser = @browser # new browser instance could have been created inside scenario via Watir::IE.attach()
  if scenario.failed?
    screenshot_filename = File.join(Dir.tmpdir, "Scenario '" + scenario.name.gsub(/[\/\\?*:|<>]/, "_") +
        "' failed @ " + Time.now.strftime("%Y-%m-%d %H%M%S"))
    # Take screenshot
    if defined?(browser.driver)
      browser.driver.save_screenshot(screenshot_filename + ".png")
      #embed("screenshot.png", "image/png")
    elsif ENV["BROWSER"] == 'IE'
      # Dump screenshot via Print Screen
      # browser.maximize
      # VK_SNAPSHOT = 0x2C
      # browser.send_keys(VK_SNAPSHOT)
      #TODO It's broken in Watir 1.9.2. Work with Watir to get a fix.
#
      # word = WIN32OLE.new('Word.Application')
      # word.Documents.Add
      # word.Selection.Paste
      # word.ActiveDocument.SaveAs(screenshot_filename + ".doc")
      # word.ActiveDocument.Close
      # word.Quit
    end

    exit 1 unless ENV["EXIT_ON_FAILURE"] == 'N'
 end
end
-- 
DK 
AIM: DKroot1, Skype: DKroot

aslak hellesoy

unread,
Aug 1, 2011, 6:49:03 PM8/1/11
to cu...@googlegroups.com
On Tue, Aug 2, 2011 at 12:43 AM, Dmitriy Korobskiy <dkr...@gmail.com> wrote:
> On 7/19/11 5:17 AM, Matt Wynne wrote:
>
> On 19 Jul 2011, at 00:37, planon wrote:
>
> Cucumber keeps executing even after a step definition has failed, and
> it outputs not only the failing steps, but also the passing ones. What
> option do I need to use in order to ensure that Cucumber stop
> executing as soon as it has encountered a failing step?
>
> [skipped]
>
> You can use an After hook:
>
> After do |scenario|
> exit 1 if scenario.failed?
> end
>

Actually, it's this:

After do |scenario|
Cucumber.wants_to_quit = true if scenario.failed?
end

Aslak

> --
> You received this message because you are subscribed to the Google Groups
> "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to
> cukes+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cukes?hl=en.
>

aslak hellesoy

unread,
Aug 2, 2011, 6:50:28 AM8/2/11
to cu...@googlegroups.com
Please send your mail again, without responding to an unrelated thread.

Aslak

On Tue, Aug 2, 2011 at 10:03 AM, Aditya tondon <adity...@gmail.com> wrote:
> Guys
>
> I have a issue , i am new to cucumber, capybara and jruby ,bundle ,
> the company where i work they are using the above mentioned combination to
> test the websites.
>
> My question . : i just to get up to a speed with writing effective step
> definations , for this what should be my approach ?
>                        is there any book or reference material which i can
> refer ?
>
>
> if i am not asking much ,what would be the use  of capybara  in this as i am
> quite new to it .
>
> and how much i need to know about the capyabara . can any one provide me a
> live example to help me start implenting it .
>
> The stage where i am right now is :
>  a) Have installed above three applications and waiting to try some example
> for a real hands on.
>
>
>

Aditya tondon

unread,
Aug 2, 2011, 5:03:57 AM8/2/11
to cu...@googlegroups.com

Dmitriy Korobskiy

unread,
Aug 2, 2011, 12:07:46 PM8/2/11
to cu...@googlegroups.com
On 8/1/11 6:49 PM, aslak hellesoy wrote:
Actually, it's this:

After do |scenario|
  Cucumber.wants_to_quit = true if scenario.failed?
end

Aslak

That worked! Thanks!
Reply all
Reply to author
Forward
0 new messages