Read more

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

Arne Hartherz
October 22, 2012Software engineer at makandra GmbH

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

Details

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2012-10-22 17:55)