×
☰ See All Chapters

Cucumber Background

  • A background section in a feature file allows you to extract a set of steps that are common to all scenarios in the file. If all scenarios have set of common steps, you can move them up into a Background element. 

  • Example without Background 

Feature: ATM application

  Scenario: Withdraw money using debit card

    Given I use my debit card

    And I insert the card, enter the correct PIN

    And I Press enter

    When I make a withdrawal

    Then I get the expected amount of money from the ATM

 

  Scenario: Transfer money using debit card

    Given I use my debit card

    And I insert the card, enter the correct PIN

    And I Press enter

    When I make a transfer

    Then Money should be transferred to target account

  • Example with Background 

The above feature can be written using Background as below:

Feature: ATM application

  Background:

    Given I use my debit card

    And I insert the card, enter the correct PIN

    And I Press enter

  Scenario: Withdraw money using debit card

    When I make a withdrawal

    Then I get the expected amount of money from the ATM

  Scenario: Transfer money using debit card

    When I make a transfer

    Then Money should be transferred to target account

  • During runtime, the steps in the background are executed at the beginning of each scenario, just as they were added in each scenario. What we have done is made each individual scenario. 

  • You can have a single Background element per feature file, and it must appear before any of the Scenario or Scenario Outline elements. You can give Background a name, and put a multiline description before the first step.  

 


All Chapters
Author