Write Great Cucumber Tests

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

January 31, 2026 · 5 min read · Testing Guide

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted August 9, 2021

Write Great Cucumber Tests

Use Cucumber with best practices in your automated test see that your automation experience will be successful and that you ’ ll get the maximal ROI.

quote

Behavior-driven development (BDD) is an Agile software development process encouraging collaboration between developers, QA and non-technical participant in a software task. Cucumber is an example of a JavaScript (JS) methodology that enables BDD. It assist to lay a test automation foundation by embracing coding standards and blueprint patterns that will be habituate in the mechanisation development process.

What is Cucumber Testing?

Cucumber is an open-source software testing creature that is written in Ruby. Cucumber enable the writing of tryout cases that helps them be easily tacit. Cucumber can be thought of as a testing framework, drive by manifest English textbook. It will serve as documentation, an automated trial, and an aid to development.

Writing Features

Feature filesin Cucumber help non-technical stakeholder engage with the testing that is be done. Feature file are frequently located at the root of the /features directory, and concentrate on a individual business value. They may be group in a subfolder if they describe a common target. The sorted filename can represent the action and context of the subfolder.

Background stepare run before a scenario. Scenarios run severally, and should miss dependencies on other scenarios. Declarative, rather than imperative, scenario will better show the implementation details of the application. Additionally,tagson features are inherited by scenario which reduces tag redundancy.

Scenarios may be initialized by usingGivenstatements which put a system into a know state before user interaction.Whenstatements describe a key activity the exploiter performs, and aThenstatement discover the outcome. Avoid details in a scenario that are not required.

Recyclable objects aid in the efficiency ofCucumber tests. Because objective interact, they need a measured design.

Write Step Definitions

Step definitions(which Cucumber utilize to translate plain-text Gherkin files into actions) are defined in the Ruby file underfeatures/step_definitions/ * _steps.rb. Steps should be reusable by obviate the internalization of hard-coded argument.

Step definitions can be reuse in other step definitions by callsteps helper, but this use is not recommended.many_steps helperis a best method since it create a meaningful plenty trace possible should a test crash.

When To Use Page Object Pattern

A superior reuse approaching involvesPage Object Pattern, which is map to a Ruby class file. Each method within the class represents a page objective on the page. If UI coupling is centralized in the plan of the objects, the project is more easily maintainable. It ends up with only one location where changes are postulate rather than multiple places throughout the step definitions.

The rspec-expectations libraryis bundled with many built-in matchers for testing validation. Each of the matcher can be used withexpect (..) .to or expect (..) .not_toto define confident and negative expectations respectively on an object under trial. Rspec-expectations matchers work good than Selenium matchers in a Ruby-based examination system like Cucumber.

Cucumber contour requires certain files. Pinning the version of these file stops auto-updating which disrupts tryout mechanisation.

cucumber.ymlsimplifies command-line execution by defining recyclable profiles within the cucumber.yml file.Gemfileis used tomanage dependenciesfor a Ruby project.Rakefileis a package task management and establish automation instrument. It let the spec of tasks, description of colony, and group tasks in namespaces.support/env.rbconfigures the environment of Cucumber. It simplifies changes in Cucumber ’ s execution environment such as local to remote.

The Sequence of a Cucumber Test

The construction of a complete test would flow in this manner:

  • Write .featurefiles with scenarios and a given-when-then structure for each scenario

  • Write step definition files where you define functions that match the step in your scenarios

  • Implement these functions as you like.

  • Run the exam by executing the cucumber-js executable in thenode_modules/.bin folder

A Complete Example

A uncomplicated “ hello world ” Cucumber test will start with the feature description. Italicized textbook is entered to create the test.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

# feature/hello_cucumber.feature

Feature: Hello Cucumber

I need a greeting to be shown


Scenario: User sees the welcome substance

When I go to the homepage

Then I should see the welcome content


Scenario instructions
are calledsteps, and they motor the tests.

For each step in the scenario, Ruby code will be executed. The step definitions (the blocks of codification) are placed in a file namehello_steps.rb. For this, it will contain:

When (/^I go to the homepage $ /) do

visit root_path

endit


Then (/^I should see the welcome substance $ /) do

expect (page) .to have_content (& quot; Hello Cucumber & quot;)

End

Each line in the Cucumber lineament file, called a scenario step, is connect with its corresponding step definition. This matches the footstep definition thread with the use of a regular expression.

Getting to the homepage means the user will see theroot_path(which is standard Rails terminology, and it ’ s something defined in config/routes.rb file). In the expectation step, a check is made that the homepage contain the “ Hello Cucumber ” text.

# config/routes.rb

Rails.application.routes.draw do

root & # x27; welcome # index

end


# app/controllers/welcome_controller.rb

class WelcomeController & lt; ApplicationController

def index

end

end

# app/views/welcome/index.html.erb


Then running all of this provides:

$ cucumber -s

Using the default profile…

Feature: Hello Cucumber


Scenario: User realize the welcome content

When I go to the homepage

Then I should see the welcome message


1 scenario (1 surpass)

2 steps (2 passed)

The -sflag tells Cucumber to hide the location of each step definition, which is the default behavior. The example has all the basic Cucumber ingredient.

allows for squad collaboration early in the planning and evolution phase. Keep the figure down. Ask yourself and the team: “ If I can write a low-level test for this user story, should I compose an UI acceptance test? ” We should automatize entirely the things that need to be automated at the UI level. It is a known fact that low-level tests are more true liken to UI tests. Cucumber is codification, and all types of programme require thoughts upfront before writing automated tests. It is so important to lay a test automation fundament that adopt coding standards and follows design shape that everyone will follow during automation development.

Published:
Aug 9, 2021
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rightfield reserved. SAUCE and SAUCE LABS are registered trademarks own by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free