Writing Feature Files with Cucumber: A Beginner’s Guide

Anshita Bhasin
5 min readApr 5, 2023

Cucumber is a widely-used behavior-driven development (BDD) framework that promotes collaboration between developers and non-technical stakeholders for defining and testing software requirements. Its plain-text format, called Gherkin, facilitates describing an application’s behavior in terms of scenarios and steps in a user-friendly language.

Here’s a beginner’s guide to writing feature files with Cucumber:

1) Understanding Gherkin: The Language Utilized in Cucumber’s Feature Files

To write feature files in Cucumber, it’s important to understand the Gherkin syntax, which is the language used for describing an application’s behavior in plain English. Gherkin has a simple syntax that includes the following key elements:

  • Feature: Describes the overall feature that you are testing.
  • Scenario: Describes a specific scenario or test case within the feature.
  • Given: Describe the initial state or setup for the scenario.
  • When: Describes the action that triggers the behavior you are testing.
  • Then: Describes the expected outcome or result of the scenario.
  • And: Allows you to add additional Given, When, or Then statements to a scenario.
  • But: Allows you to add an exception or negative case to a scenario.

2) Feature file

Feature files are text files with the .feature extension that are used to describe a particular functionality of your application that requires testing.

To get started with feature file writing, it’s important to focus on a single functionality of your application, such as creating a new user or logging in. Below is an example of a feature file that tests the login functionality of a web application:

Feature: Login Functionality
As a user
I want to be able to log in to my account
So that I can access my profile and settings

Scenario: Valid Login
Given I am on the login page
When I enter valid credentials
And I click on the login button
Then I should be logged in to my account

Scenario: Invalid Login
Given I am on the login page
When I enter invalid credentials
And I click on the login button
Then I should see an error message

In the above example, The feature file includes two scenarios: one for a valid login and one for an invalid login. Let’s break down how the key elements of Gherkin syntax are used in each scenario:

  • Feature: This describes the overall feature being tested, which is the login functionality in this case.
  • Scenario: This defines a specific test case or scenario within the feature. In this example, we have two scenarios: one for a valid login and one for an invalid login.
  • Given: This outlines the initial state or setup for the scenario. In both scenarios, we start on the login page.
  • When: This describes the action that triggers the behavior being tested. In the valid login scenario, we enter valid credentials and click on the login button. In the invalid login scenario, we enter invalid credentials and click on the login button.
  • Then: This specifies the expected outcome or result of the scenario. In the valid login scenario, we should be logged in to our account. In the invalid login scenario, we should see an error message and still be on the login page, but we should not be logged in to our account.
  • And: This allows us to include additional Given, When, or Then statements within a scenario. In both scenarios, we use the “And” keyword to add additional steps after the initial Given and When statements.

3) Use data tables and examples

By utilizing data tables and examples in your scenario, you can test multiple inputs or scenarios at once. Below is an example of a feature file that explains the use of examples:

Scenario Outline: Check login functionality
Given I am on the login page
When I enter "<username>" and "<password>"
Then I should be logged in successfully

Examples:
| username | password |
| Anshita | 123456 |
| Alice | qwerty |

In this example, the Scenario Outline defines a scenario for checking login functionality. It uses placeholders <username> and <password> to indicate where the username and password inputs will be inserted. The Examples section provides a table of inputs and expected outcomes for each set of inputs.

When executed, Cucumber will run the scenario outlined in the Scenario Outline for each set of inputs defined in the Examples section.

Examples allow you to define a set of inputs and expected outcomes in a tabular format. They can be used within a Scenario Outline, or as standalone scenarios. For example:

Scenario: Check login functionality for multiple users
Given I am on the login page
When I enter "Anshita" and "123456"
Then I should be logged in successfully

When I enter "Alice" and "qwerty"
Then I should be logged in successfully

In this example, the scenario defines multiple sets of inputs and expected outcomes for testing login functionality for different users. When executed, Cucumber will run the scenario for each set of inputs defined.

4) Best Practices for Creating Feature Files

Feature files should be concise and readable so that anyone can understand what they are testing. Keep each scenario focused on a single behavior and avoid technical jargon.

Below are the tips to create readable and easy-to-understand feature files that effectively describe the behavior of your application in plain English.

(i) Use meaningful and descriptive feature and scenario names:

Choose feature and scenario names that accurately reflect the behavior being tested. Use descriptive language that conveys the purpose and context of the test.

(ii) Write scenarios in a clear and simple format:

Use the Given-When-Then format to clearly describe the steps involved in each scenario. Keep the steps simple and focused on the behavior being tested.

(iii) Use tags to organize and group scenarios:

Use tags to group scenarios based on their purpose or functionality. This makes it easier to run specific sets of tests and organize the results.

(iv) Use comments to provide additional context:

Use comments to provide additional context and explain the purpose or intent of a scenario. This can be helpful for other team members who may need to read and understand the feature file.

(v) Collaborate with stakeholders to write effective feature files:

Involve stakeholders in the process of writing feature files to ensure that the tests accurately reflect the requirements and expectations of the application. This can lead to more effective tests and fewer errors or misunderstandings.

Conclusion:

In conclusion, mastering the art of writing feature files in Cucumber can make a huge difference. With Gherkin’s simple syntax, concise and readable feature files, and the ability to use data tables and examples, Cucumber provides a powerful way to collaborate and automate your testing.

By following these techniques, you can improve your software's quality and increase your development team's efficiency.

Thanks for reading. Happy Learning! — AB

Thanks, Naveen AutomationLabs for the guidance.

Anshita Bhasin
Sr. Automation Engineer

GitHub | LinkedIn | Twitter | Youtube

--

--

Anshita Bhasin

QA Chapter Lead at Proptech company in Dubai.. Here to share and learn new things! Youtube => ABAutomationHub