Learning About Cucumber

Firman Wihanarko
Qasir
Published in
3 min readOct 7, 2022

Chapter 1: Getting Started with Cucumber

About Cucumber

Cucumber is a tool which runs executable specifications of the software. Specifications or called as “features” are written in structured natural language. Cucumber executes a feature by mapping each of its steps to a “step definition” written in the programming language supported by that implementation of Cucumber. Cucumber is implemented in many programming languages including Ruby, Java, and Javascript. It is also translated into many human languages.

Cucumber was written to support the agile methodology called Behavior-Driven Development (BDD). In BDD, one begins development outside-in by writing acceptance tests that describe the software’s functionality from the user’s point of view (rather than from a programmer’s point of view such as in unit tests). Cucumber features serve as these acceptance tests. In general, Cucumber features are human-readable documentation which is also an executable test suite, meaning that documentation and tests always agree.

Cucumber is useful in communicating with non-programmer stakeholders about documentation and tests. It also allows programmers to write tests at a conceptual level without irrelevant programming-language concerns. Cucumber is most often used to specify and test web applications, using a browser driver such as Selenium or PhantomJS. However, it can be used with any software that can be executed and whose state or results can be determined from the programming language that a Cucumber implementation supports.

Other Documentation

Official documentation is at https://cucumber.io/docs. Documentation generated from the Cucumber features which describe Cucumber implementations is at:

• JavaScript: https://relishapp.com/cucumber/cucumber-js/docs

• Ruby: https://relishapp.com/cucumber/cucumber/docs

https://relishapp.com/explore includes some other Cucumber-related tools and examples, although not, unfortunately, Cucumber-JVM.

This topic

This topic should only give a few examples which introduce the reader to Cucumber concepts. Other sections will give complete examples of installation, command-line, and IDE usage, features, step definitions, etc.

Examples:

A Cucumber feature

Cucumber uses Gherkin syntax to describe your software’s behaviors in structured natural language. As such Cucumber is not a test framework (a common misunderstanding), but a system documentation framework, not very different from others like Use Case Scenario. The common misunderstanding is due to the fact Cucumber documentation can be automated in order to ensure it reflects the real system behavior. A Cucumber documentation suite is composed of Features, each describing a feature of your software, written in Gherkin and hosted in its own file. By organizing those files into a directory structure you can group and organize features:

  • login/ =
  1. login.feature
  2. spv_login.feature
  3. operator_login.feature
  • GettingStarted/ =
  1. beranda_menu.feature
  2. beranda_navbar.feature
  3. operator_navbar.feature

Each Feature is a plain text file composed by an optional, unstructured, purely informational introductory section and one or more Scenarios, each one representing a usage condition or use case.

Example:

Each line beginning with Given, When, And, But or Then is called a Step. Any step can begin with any of those words regardless of order, but it is conventional to use them in the most natural way possible. Features can also be organized via Tags, annotations the editor can put on a Feature or a Scenario to further categorize it. Executability of a Feature is achieved via glue code which can be written in many different languages (Java, Ruby, Scala, C/C++): each Step is matched against the glue code in order to identify Step Definitions (commonly abbreviated to StepDef) via regular expressions. Every Step can have only one associated Step Definition. When a Feature is executed each composing Scenario is executed, meaning each StepDef matching the Steps in every Scenario gets executed.

For the next chapter, I will explain the Features on cucumber, see you later …

--

--