Cucumber pitfall: "Around" does not apply to your "Background" steps

Updated . Posted . Visible to the public.

Around will not happen until after a feature's Background has been processed. Use Before and After to avoid that.

Details

Consider this Cucumber feature file:

Feature: Something that needs to be tested

  Background:
    Given a user
      And I sign in        
    
  Scenario: Sign out
    When I sign out
    Then I should see "Signed out"
  
  Scenario: Something else
    # ...

Now, assume you have these step definitions:

Around do
  puts "** Around: before yield"
  yield
  puts "** Around: after yield"
end

Before do
  puts "** Before"
end

Running the above feature will give you (roughly) this Cucumber output:

** Before
Given a user
And I sign in
** Around: before yield
When I sign out
Then I should see "Signed out"
** Around: after yield

As you can see, your Around code did not happen until after Background was processed.

This can lead to serious trouble if you are not aware of that. For example: Using an Around step to switch things off will not switch them off until the Background steps have happened. If you do something like that, use Before and After steps instead.

Arne Hartherz
Last edit
Dominik Schöler
Keywords
order
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2012-10-22 15:55)