Cucumber Background Tutorial

Reading Time 2 minutes

When writing multiple scenarios in a Feature file we can easily end up writing the same ‘introductory’. However we can reduce the duplication using Cucumber Background. Let’s have a look at how we can use Cucumber Background to run common steps.

Cucumber Background

Lets have a look at a Feature file with multiple Scenarios which use similar steps:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Git Tutorial" page
Then the page title should be visible

If you have a look at the above Scenarios then you can see that we repeat the same ‘Given’ step across all Scenarios. This means that if we have to write more Scenarios then we will end up writing the same test step for each Scenario. This is where we can reduce running the same test steps via Cucumber Background. Lets have a look at how we can use Cucumber Background to help us:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Background:
Given I navigate to TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to "Git Tutorial" page
Then the page title should be visible

The Background section in a Feature file is designed to run test steps before each Scenario in the Feature file. For instance in the above Feature file the Background section will run before every Scenario. This means every Scenario will automatically run the ‘Given’ step for each Scenario.

And that’s it. In this tutorial we have learned how to use Cucumber Background and apply it to a Feature file. We have seen how the Background script can be used to reduce test step duplication and also increase Feature file management.

Video Tutorial on YouTube

cucumber background

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.