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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)