Getting started with Cucumber BDD for Automation Testing
Tags: bdd, cucumber, automationtesting, softwaretesting
Published: 2018-09-18 07:33:55

---
title: Getting started with Cucumber BDD for Automation Testing
published: true
tags: BDD, Cucumber, AutomationTesting, softwaretesting
canonical_url: https://www.katalon.com/resources-center/blog/cucumber-bdd-automation-testing/
---
>**Due date: 23:59 p.m Oct 25th, 2021**
>**The State of Quality Report 2021**
>_Wouldn’t it be great to know how peers and experts are staying ahead of their quality assurance game? That’s why we're creating The State of Quality Report 2021 to collect the best QA practices from you, professionals of all levels.
We are happy to offer **the first 100 respondents a $30 Amazon gift card**, along with the final report from the survey results. [Raise your voice!](https://www.research.net/r/medium-devto)_

<h2>Introduction</h2>
<span style="font-weight: 400;">In recent years, there have been more software teams increasingly implementing the Agile software methodology in their development process to adapt to this fast-changing market. This trend challenges testing teams to manage test cases and test scripts which have to be maintained according to changing requirements. Finding an appropriate testing method right from the beginning is one of the key elements of the successful Agile software project.</span>
<h2>What is Cucumber?</h2>
<span style="font-weight: 400;">In fact, many Agile teams have successfully implemented the Behavior-Driven Development (BDD) method for their testing process using Cucumber tool. So, what is Cucumber? And why is it the suitable strategy suggested for Agile projects, along with BDD?</span>

<span style="font-weight: 400;"><a href="https://cucumber.io/" target="_blank" rel="noopener">Cucumber</a> is a tool used to run automated acceptance tests created in a BDD format. One of its most outstanding features of the tool is the ability to carry out plain-text functional descriptions (written in the language called Gherkin) as automated tests. Let’s take a look at the below example:</span>
<pre class="lang:default decode:true">Feature: Update password
  Scenario: Admin user can update the user password
Given I am in the HR system with an Admin account
When I update password of another user
Then I receive a message for updating password successfully
And user password is updated to the new password</pre>
<span style="font-weight: 400;">This incredible feature of Behavior-Driven Development (BDD) approach with the advantages as below:</span>
<ul>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Writing BDD tests in an omnipresent language, a language structured around the domain model and widely used by all team members comprising of developers, testers, BAs, and customers.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Connecting technical with nontechnical members of a software team.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Allowing direct interaction with the developer’s code, but BDD tests are written in a language which can also be made out by business stakeholders.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Last but not least, acceptance tests can be executed automatically, while it is performed manually by business stakeholders.</span></li>
</ul>
<h2><b>Cu</b><b>cumber helps improve communication</b></h2>
Cucumber helps improve communication between technical and non-technical members in the same project. <span style="font-weight: 400;">Let’s have a look at the below requirement and its automation tests:</span>
<pre class="lang:default decode:true ">As an Admin User,
I would like to change the password of other user's accounts.
Feature: Update password
  Scenario: Admin user can update the user password
    Given I am in the HR system with an Admin account
    When I update password of another user
    Then I receive a message for updating password successfully
    And user's password is updated to the new password</pre>
<span style="font-weight: 400;">With TestNG, the above test scenario can be implemented as below:</span>
<pre class="lang:default decode:true ">@test
public void testAdminUserCanUpdateUserAccountPassword() {
  // create users 
  User userAdmin = new User(UserRole.ADMIN, username, password);
  User user = new User(UserRole.VIEWER, user_username, user_password);
        
  // use Admin user to update another user password
   String message = userAdmin.updatePassword(user, user_new_password);
   
// verify password changed
   Assert.assertEquals(message, "Password changed successfully");
   Assert.assertEquals(user.getPassword(), user_new_password);
}</pre>
<span style="font-weight: 400;">The same test case can be written using Cucumber:</span>
<pre class="lang:default decode:true ">Feature: Update password
  Scenario: Admin user can update the user password
    Given I am in the HR system with an Admin account
    When I update password of another user
    Then I receive a message for updating password successfully
    And user's password is updated to the new password</pre>
Both automation test scripts above perform well to complete the test automatically. But do all testers of your team make out these tests? Can other business analysts and other stakeholders use these tests again at the acceptance testing (AT) stage?

<span style="font-weight: 400;">The automation test with TestNG may be difficult for most manual testers and BAs to catch up with. Moreover, it is impossible to use this test again for AT. As a result, based on these flaws mentioned before, this can not be considered as a suitable method.</span>

<span style="font-weight: 400;">In contrast, <a href="https://www.katalon.com/resources-center/blog/cucumber-bdd-automation-testing/">Automation Testing with Cucumber BDD</a> is created in a business domain language or in natural language, which can be easily made out by all members of the software project team. Communication is crucial for any development team, especially in the Agile team. There are usually many continuous chats, discussions, or even arguments happening among developers and testers in order to figure out what the correct behavior of a feature is. By using Cucumber, the same feature specification is now used for developing by developers, for testing by testers. It is considered to be a powerful tool because it can help lower the risk for misunderstanding as well as the communication breakdown.</span>
<h2><b>Cucumber is an Automated Acceptance Testing Tool</b></h2>
<span style="font-weight: 400;">The acceptance test typically is carried out by BAs/customers to make sure that the development team has built exact features. Typical activity in this testing stage is verifying the system against the original requirements with specific, real data from production. Cucumber testing not only follows the requirements as its test scenarios but also helps BAs or Product Manager to easily adjust test data. Here is a demonstration with a little adjustment:</span>
<pre class="lang:default decode:true">As an Admin User,
I would like to change the password of other user's accounts.
            
Feature: Update password
  Scenario: Admin user can update the user password
    Given I am in the HR system with an Admin account
    When I update password of another user
    Then I receive a message for updating password successfully
    And user's password is updated to the new password</pre>
<span style="font-weight: 400;">The automation test written in Cucumber framework:</span>
<pre class="lang:default decode:true">Scenario Outline: Verify Updating user password feature
  Given I am in the HR system with "&lt;account_type&gt;" account
  And there is another user with "&lt;old_password&gt;" password
  When I update password of the user to "&lt;new_password&gt;"
  Then I got the message "&lt;message&gt;"
  And the user password should be "&lt;final_password&gt;"

Examples:
|account_type  |old_password  |new_password |message                |final_password |
|Admin         |$Test123      |@Test123     |Password changed..     |@Test123       |
|Viewer        |$Test123      |@Test123     |Invalid right access.. |$Test123       |</pre>
<h2>All testers can take part in automation test with Cucumber</h2>
<span style="font-weight: 400;">In addition to improving communication among members of the same testing team, Cucumber also helps leverage tester’s skills efficiently. Expertise gap always exists in every organization. In other words, there are some testers who have high technical expertise in programming utilizing automated testing, while others performing manual testing with limited programming skills in the same team. Thanks to Cucumber, all testers, no matter what their skill levels are, can participate in the process of performing automation tests.</span>

<span style="font-weight: 400;">Let’s take a look at the above example:</span>
<ul>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Any tester who is aware of the business logic and workflow can write feature files, add more scenarios, and test datasets.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Any tester who has a basic knowledge of programming and know how to create objects, access properties, call methods, can generate step definitions.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Any tester with higher programming skill level can take part in the process of making a framework, define data source connection and so on.</span></li>
</ul>
<span style="font-weight: 400;">There are still a few potential issues when implementing Cucumber:</span>
<ul>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Cucumber helps run test scenarios specified in a plain text file using business domain knowledge. Thus, the usage of languages and the perception of the one who creates the test might directly influence the test scenarios, leading to the risk for misunderstanding. Test scenarios should be presented clearly, and their implementation should perform accurately for each step. For instance, when you want to verify the Search feature on Google, the test should be:</span></li>
</ul>
<pre class="lang:default decode:true">Scenario: performing a search on google
Given I am on "www.google.com" site
When I search for "Cucumber and BDD"
Then ...</pre>
<span style="font-weight: 400;">These steps may be incorporated to have the following test:</span>
<pre class="lang:default decode:true ">Scenario: performing a search on google
When I search for "Cucumber and BDD"
Then ...</pre>
<span style="font-weight: 400;">The stages of the Cucumber tool are performed in an ordinary language. They can be used again in various test scenarios. This helps reduce the effort to create tests. However, maintaining the test to be both readable and reusable is really a big challenge. If the test is written at a very high level for any stakeholders to make out; few steps (bold) can be reused:Both the above scripts are correct; however, the second one is not apparent because it does too much more than expected: opening Google’s website and searching with the specified text. Imagine if you want to extend the test to search more texts, you may repeat the above step, and the Google site is consequently opened twice. If you do not strictly follow the requirement, the Cucumber testing tool will cause misunderstanding sooner or later and be so difficult to maintain when being extended.</span>
<pre class="lang:default decode:true ">Feature: Update password
  Scenario: Admin user can update the user password
    Given I am in the HR system with an Admin account
    When I update password of another user
    Then I receive a message for updating password successfully
    And user's password is updated to the new password

  Scenario: Viewer user cannot update the user password
    Given I am in the HR system with a Viewer account
     When I update password of another user
     Then I receive a message for not able to update the user password
     And user's password remains the same</pre>
<span style="font-weight: 400;">In contrast, if the test is generic and can be reused, i.e., verifying updating user’s Last Name, non-technical stakeholders will have difficulty in catching up with and performing Acceptance Tests:</span>
<pre class="lang:default decode:true ">Scenario: Admin user can update user password:
  Given I am in the "$System.HR_Page" with "admin@test.com" username
and "$Test123" password
  And there is another user in "$System.HR_Page" with "user@test.com" 
username and "$Test123" password
  When I update "$UserTemplate.Password" of "user@test.com" user to"@Test123"
  And I save the response message as "response_message"
  Then "$response_message" should be "Password changed successfully"
  And the  "user@test.com" user's "$UserTemplate.Password" should be"@Test123"</pre>
During the testing process, you have to adjust test scenarios regularly until they completely reach an acceptable balance where all members can understand and reuse.
<pre class="lang:default decode:true ">Scenario: Verify Updating user password feature
  Given I am in the HR system with "Admin" account
  And there is another user with "$Test123" password
  When I update password of the user to "@Test123"
  Then I got the message "Password changed successfully."
  And the user password should be "@Test123"</pre>
Or with some more test data:
<pre class="lang:default decode:true">Scenario Outline: Verify Updating user password feature
  Given I am in the HR system with "&lt;account_type&gt;" account
  And there is another user with "&lt;old_password&gt;" password
  When I update password of the user to "&lt;new_password&gt;"
  Then I got the message "&lt;message&gt;"
  And the user password should be "&lt;final_password&gt;"

Examples:
|account_type |old_password |new_password |message           |final_password |
|Admin        |$Test123     |@Test123     |Password changed..     |@Test123  |
|Viewer       |$Test123     |@Test123     |Invalid right access.. |$Test123  |
</pre>
<h2><strong>Important notes for the testing team who wants to get started with Cucumber</strong></h2>
<ul>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Consider automation tests as essential as a real project. The code should follow coding practice, convention, etc.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">An appropriate editor tool should be considered. This editor should help debug and edit feature files in standard text format. Aptana (free editor), RubyMine (commercial editor) and <a href="https://www.katalon.com" target="_blank" rel="noopener">Katalon Studio</a> are suitable options which completely support BDD-based Cucumber.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Last but not least, make feature files an actual “communication” layer where you can store received test data and format test data. Domain business logic is not contained.</span></li>
</ul>
<span style="font-weight: 400;">To conclude, by offering the real communication layer on top of a robust testing framework, Cucumber not only can execute automation test on a wide-ranging testing demand from backend to frontend but also create deep connections among members of the testing teams. This feature is hardly found in other testing frameworks. With many years of expertise in the field of building and applying automation tests, I suggest that Cucumber for Web UI and Web services testing should be applied for Agile software projects. <a href="https://www.katalon.com/faqs/#katalon-studio-support-bdd" target="_blank" rel="noopener">Using Cucumber-based BDD with Katalon Studio as an editor</a> is a right choice which can significantly help reduce the effort to execute regression tests.</span>

&nbsp;

<em><span style="font-weight: 400;">Author: Trong Bui - QAA at KMS-Technology</span></em>