Cucumber (software)#Syntax

{{short description|Software tool}}

{{refimprove|date=October 2016}}

{{Infobox software

| name = Cucumber

| logo =

| screenshot =

| caption =

| developer = Aslak Hellesøy,{{cite web|url=http://aslakhellesoy.com/ |title=Aslak Hellesøy |publisher=Aslakhellesoy.com |access-date=2012-01-24}} Joseph Wilk,{{cite web|url=http://blog.josephwilk.net/ |title=Joseph Wilk | on AI, The Web, Usability, Testing & Software process |publisher=Blog.josephwilk.net |access-date=2012-01-24}} Matt Wynne,{{cite web|url=http://blog.mattwynne.net/ |title=Tea-Driven Development |publisher=Blog.mattwynne.net |access-date=2012-01-24}} Gregory Hnatiuk,{{cite web|url=https://github.com/ghnatiuk |title=ghnatiuk's Profile |publisher=GitHub |access-date=2012-01-24}} Mike Sassak{{cite web|url=https://github.com/msassak |title=msassak's Profile |publisher=GitHub |access-date=2012-01-24}}

| latest release version = {{wikidata|property|edit|reference|P348}}

| latest release date = {{wikidata|qualifier|P348|P577|maxvals=1}}

| programming language = Ruby

| operating_system = Cross-platform

| genre = Behavior driven development framework / Test tool

| license = MIT License

| website = {{URL|https://cucumber.io}}

}}

Cucumber is a software tool that supports behavior-driven development (BDD).{{cite web |url=http://pragprog.com/book/hwcuc/the-cucumber-book |title=The Pragmatic Bookshelf | The Cucumber Book |publisher=Pragprog.com |access-date=2012-01-24 |archive-url=https://web.archive.org/web/20120121022646/http://pragprog.com/book/hwcuc/the-cucumber-book |archive-date=2012-01-21 |url-status=dead }}{{cite book|url=https://pragprog.com/book/srjcuc/the-cucumber-for-java-book |title=The Pragmatic Bookshelf | The Cucumber For Java Book |publisher=Pragprog.com |author1=Rose, Seb|author2=Wynne, Matt|author3=Hellesøy, Aslak|date=15 February 2015 |access-date=2019-04-28}}{{cite web |title=What is Cucumber? |website=cucumber |url=https://cucumber.io/docs/guides/overview/ |access-date=2019-06-08}}{{cite web |title=The world's most misunderstood collaboration tool |author=Aslak Hellesøy |website=cucumber |url=https://cucumber.io/blog/the-worlds-most-misunderstood-collaboration-tool/}}{{Cite book |last1=Nagy |first1=Gáspár |url=http://leanpub.com/bddbooks-discovery |title=The BDD Books - Discovery |last2=Rose |first2=Seb |date=2017-03-07 |publisher=Leanpub |isbn=978-1983591259}}{{Cite book |last1=Rose |first1=Seb |url=http://leanpub.next/bddbooks-formulation |title=The BDD Books – Formulation |last2=Nagy |first2=Gáspár |date=2021-04-27 |publisher=Leanpub |isbn=979-8723395015}} Central to the Cucumber BDD approach is its ordinary language parser called Gherkin. It allows expected software behaviors to be specified in a logical language that customers can understand. As such, Cucumber allows the execution of feature documentation written in business-facing text.{{Cite book|title=Engineering Software as a Service |last1=Fox|first1=Armando|last2=Patterson|first2=David|publisher=Strawberry Canyon|year=2016|isbn=978-0-9848812-4-6|pages=218–255}} It is often used for testing other software.{{Cite web|url=https://www.ibm.com/developerworks/library/a-automating-ria/|title=Automated testing with Selenium and Cucumber|date=2013-08-06|website=www.ibm.com|language=en|access-date=2017-02-09}} It runs automated acceptance tests written in a behavior-driven development (BDD) style.{{Cite book|title=Objects, Models, Components, Patterns |last1=Soeken |first1=Mathias |last2=Wille|first2=Robert|last3=Drechsler|first3=Rolf|date=2012-05-29|publisher=Springer Berlin Heidelberg|isbn=9783642305603|editor-last=Furia|editor-first=Carlo A.|series=Lecture Notes in Computer Science |pages=269–287 |language=en|doi=10.1007/978-3-642-30561-0_19|editor-last2=Nanz|editor-first2=Sebastian}}

Cucumber was originally written in the Ruby programming language{{cite web |url=http://pragprog.com/book/achbd/the-rspec-book |title=The Pragmatic Bookshelf | The RSpec Book |publisher=Pragprog.com |date=2010-12-02 |access-date=2012-01-24 |archive-url=https://web.archive.org/web/20120121003856/http://pragprog.com/book/achbd/the-rspec-book |archive-date=2012-01-21 |url-status=dead }} and was originally used exclusively for Ruby testing as a complement to the RSpec BDD framework. Cucumber now supports a variety of different programming languages through various implementations, including Java{{cite web|url=https://github.com/cucumber/cucumber-jvm|title=Cucumber-jvm|publisher=cucumber|access-date=2018-03-08}} and JavaScript.{{cite web|url=https://github.com/cucumber/cucumber-js|title=Cucumber-js |publisher=cucumber |access-date=2018-03-08}}{{cite web|url=https://open.nytimes.com/no-code-no-problem-writing-tests-in-plain-english-537827eaaa6e |title=No Code? No Problem — Writing Tests in Plain English |publisher=Times Open |date=2019-02-01 |author1=Naidele Manjunath |author2=Olivier de Meulder |access-date=2019-04-29}} There is a port of Cucumber to .NET called SpecFlow,{{cite web |url=https://specflow.org/ |title=Binding Business Requirements to .NET Code |work=SpecFlow |access-date=2019-04-29}}{{cite web |url=https://github.com/techtalk/SpecFlow |title=SpecFlow |work=GitHub |access-date=2019-04-29}}{{cite book |title=Behavior-Driven Development with Cucumber |author1=Richard Lawrence |author2=Paul Rayner |publisher=Addison Wesley |date=2018}} now superseded by Reqnroll.{{cite web |last1=Nagy |first1=Gáspár |title=From SpecFlow to Reqnroll: Why and How |url=https://reqnroll.net/news/2024/02/from-specflow-to-reqnroll-why-and-how/ |website=reqnroll.net |date=8 February 2024 |publisher=Reqnroll |access-date=18 April 2024}}

Gherkin language

Gherkin is the language that Cucumber uses to define test cases. It is designed to be non-technical and human readable, and collectively describes use cases relating to a software system.{{Cite web|url=https://github.com/cucumber/cucumber/tree/master/gherkin|title=cucumber/gherkin|website=GitHub|language=en|access-date=2017-02-09}}{{cite web |title=Gherkin Syntax |url=https://cucumber.io/docs/gherkin/ |website=cucumber |access-date=2019-07-09}} The purpose behind Gherkin's syntax is to promote behavior-driven development practices across an entire development team, including business analysts and managers. It seeks to enforce firm, unambiguous requirements starting in the initial phases of requirements definition by business management and in other stages of the development lifecycle.

In addition to providing a script for automated testing, Gherkin's natural language syntax is designed to provide simple documentation of the code under test. Gherkin currently supports keywords in dozens of languages.{{cite web |title=Gherkin Supported Languages |url=https://github.com/cucumber/gherkin/blob/main/gherkin-languages.json |access-date=2021-03-21 |website=cucumber/cucumber: Cucumber monorepo - building blocks for Cucumber in various languages |via=GitHub |department=Gherkin}}

=Syntax=

Syntax is centered around a line-oriented design, similar to that of Python. The structure of a file is defined using whitespace and other control characters. Lines starting with # are considered comments, and can be placed anywhere in a file. Instructions are any non-empty and non-comment line. They consist of a recognized Gherkin keyword followed by a string.{{cite web |title=Reference |url=https://cucumber.io/docs/reference |url-status=dead |archive-url=https://web.archive.org/web/20151025214933/https://cucumber.io/docs/reference |archive-date=2015-10-25 |website=cucumber |access-date=2016-01-17}}

All Gherkin files have the .feature file extension. They contain a single Feature definition for the system under test and are an executable test script.

Here is an example of the syntax:{{cite web |title=Gherkin Reference |url=https://cucumber.io/docs/gherkin/reference/ |archive-url=https://web.archive.org/web/20240113160832/https://cucumber.io/docs/gherkin/reference/ |archive-date=2024-01-13 |website=cucumber |access-date=2024-02-11}}

Feature: Guess the word

# The first example has two steps

Scenario: Maker starts a game

When the Maker starts a game

Then the Maker waits for a Breaker to join

# The second example has three steps

Scenario: Breaker joins a game

Given the Maker has started a game with the word "silky"

When the Breaker joins the Maker's game

Then the Breaker must guess a word with 5 characters

Command line

Cucumber comes with a built-in command line interface that covers a comprehensive list of instructions. Like most command line tools, cucumber provides the --help option that provides a summary of arguments the command accepts.{{cite web|last1=Wynne|first1=Matt|last2=Hellesoy|first2=Aslak|title=The Cucumber Book|url=https://www.safaribooksonline.com/library/view/the-cucumber-book/9781941222911/f_0090.html|website=SafariBooksOnline|access-date=22 January 2016}}

$ cucumber --help

-r, --require LIBRARY|DIR Require files before executing the features.

--i18n LANG List keywords for in a particular language.

Run with "--i18n help" to see all languages.

-f, --format FORMAT How to format features (Default: pretty).

-o, --out [FILE|DIR] Write output to a file/directory instead of

...

Cucumber command line can be used to quickly run defined tests. It also supports running a subset of scenarios by filtering tags.

$ cucumber --tags @tag-name

The above command helps in executing only those scenarios that have the specified @tag-name. Arguments can be provided as a logical OR or AND operation of tags. Apart from tags, scenarios can be filtered on scenario names.

$ cucumber --name logout

The above command will run only those scenarios that contain the word 'logout'.

It is also useful to be able to know what went wrong when a test fails. Cucumber makes it easy to catch bugs in the code with the --backtrace option.

References

{{reflist}}