Skip to main content

Use Backgrounds in Behat Feature File

Often you find that several scenarios in the same feature start with a common context or steps. Background in Cucumber is used to define a step or series of steps which are common to all tests/scenarios in the feature file.

Feature file without Background

Feature: Registration, Login and MyAccount

  @smoke
  Scenario: Verify Login Functionality
    Given I am on the homepage
    And I follow "Sign in"
    When  I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"
   
  @smoke
  Scenario: Create New User
    Given I am on the homepage
    When I follow "Sign in"
    When I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"

Feature file with Background

In above 2 scenario you can observe first 2 steps in scenario are common, so above can be replaced with:

Feature: Registration, Login and MyAccount

  Background:
    Given I am on the homepage
    And I follow "Sign in"

  @smoke
  Scenario: Verify Login Functionality
    When I fill "email address textbox" with "goswami.tarun77@gmail.com"
    Then I fill "password textbox" with "Test1234"
   
  @smoke
  Scenario: Create New User
    When I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"

We use Background: keyword to represent Background section. You can see first 2 lines of scenarios added in background.

Background section will be executed before each Scenario or Scenario Outline in a feature file. There can be only single Background in a Feature file. Its good practice to add your preconditions in Background section. Position of Background also matters, you should declare it before any Scenario.

A Background is much like a scenario containing a number of steps. 

Key point to note is @BeforeScenario hook execute even before Background section. It is really necessary to understand the right usage of Background. As almost all the task can be done by hooks as well. This is why it is critical to use the background at the right place in the test.  

Any feature level dependency should be tie with the background and any scenario level dependency should be tie with hooks.

You have to think carefully before adding steps to Background because these are common for all the scenarios, there is possibility that steps are common for few and not needed for remaining or not needed for one to automate in future. 

Advantages of using Background in a Feature File

  1. Avoid Repetition and Duplication of Steps
  2. Improve Readability of Feature File
  3. Maintainability is easy, we have to modify at one place rather than all the scenarios

Tags
Submitted by tgoswami on September 16, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.