HOME

TheInfoList



OR:

Cucumber is a
software Software is a set of computer programs and associated documentation and data. This is in contrast to hardware, from which the system is built and which actually performs the work. At the lowest programming level, executable code consists ...
tool that supports behavior-driven development (BDD). Central to the Cucumber BDD approach is its ordinary language parser called
Gherkin A pickled cucumber (commonly known as a pickle in the United States and Canada and a gherkin in Britain, Ireland, South Africa, Australia, and New Zealand) is a usually small or miniature cucumber that has been pickled in a brine, vinegar, or ...
. 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. It is often used for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style. Cucumber was originally written in the
Ruby programming language Ruby is an interpreted, high-level, general-purpose programming language which supports multiple programming paradigms. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including p ...
. and was originally used exclusively for Ruby testing as a complement to the
RSpec RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in product ...
BDD framework. Cucumber now supports a variety of different programming languages through various implementations, including
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
. The open source port of Cucumber in .NET is called SpecFlow. For example
Cuke4php
an
Cuke4Lua
are software bridges that enable testing of
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
and
Lua Lua or LUA may refer to: Science and technology * Lua (programming language) * Latvia University of Agriculture * Last universal ancestor, in evolution Ethnicity and language * Lua people, of Laos * Lawa people, of Thailand sometimes referred t ...
projects, respectively. Other implementations may simply leverage the
Gherkin A pickled cucumber (commonly known as a pickle in the United States and Canada and a gherkin in Britain, Ireland, South Africa, Australia, and New Zealand) is a usually small or miniature cucumber that has been pickled in a brine, vinegar, or ...
parser while implementing the rest of the testing framework in the target language.


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. 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. Language Operations # List available languages cucumber --i18n help # List a language's keywords cucumber --i18n $LANG


Syntax

Syntax is centered around a line-oriented design, similar to that of
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
. The structure of a file is defined using whitespace and other control characters. # is used as the line-comment character, 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. 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.


Features, scenarios, and steps

Cucumber tests are divided into individual Features. These Features are subdivided into Scenarios, which are sequences of Steps.


Features

A feature is a
Use Case In software and systems engineering, the phrase use case is a polyseme with two senses: # A usage scenario for a piece of software; often used in the plural to suggest situations where a piece of software may be useful. # A potential scenario ...
that describes a specific function of the software being tested. There are three parts to a Feature * The Feature: keyword * The Feature name (on the same line as the keyword) * An optional description on the following lines Example Feature definition Feature: Withdraw Money from ATM A user with an account at a bank would like to withdraw money from an ATM. Provided he has a valid account and debit or credit card, he is allowed to make the transaction. The ATM will tend the requested amount of money, return his card, and subtract amount of the withdrawal from the user's account. Scenario: Scenario 1 Given preconditions When actions Then results Scenario: Scenario 2 ...


Scenarios

Each Feature is made of a collection of scenarios. A single scenario is a flow of events through the Feature being described and maps 1:1 with an executable test case for the system. Keeping with the example ATM withdrawal feature, a scenario might describe how a user requests money and what happens to their account. Scenario: Eric wants to withdraw money from his bank account at an ATM Given Eric has a valid Credit or Debit card And his account balance is $100 When he inserts his card And withdraws $45 Then the ATM returns $45 And his account balance is $55 In some cases, one might want to test multiple scenarios at once to perform
Equivalence partitioning Equivalence partitioning or equivalence class partitioning (ECP) is a software testing technique that divides the input data of a software unit into partitions of equivalent data from which test cases can be derived. In principle, test cases are d ...
and
Boundary-value analysis Boundary-value analysis is a software testing technique in which tests are designed to include representatives of boundary values in a range. The idea comes from the boundary. Given that we have a set of test vectors to test the system, a topology ...
. A Scenario Outline provides a technique to specify multiple examples to test against a template scenario by using placeholders. For example, Scenario Outline: A user withdraws money from an ATM Given has a valid Credit or Debit card And their account balance is When they insert their card And withdraw Then the ATM returns And their account balance is Examples: , Name , OriginalBalance , WithdrawalAmount , NewBalance , , Eric , 100 , 45 , 55 , , Gaurav , 100 , 40 , 60 , , Ed , 1000 , 200 , 800 , At runtime, the scenario is run against each row in the table. Column values are substituted for each of the named placeholders in the scenario.


Steps

The crux of a Scenario is defined by a sequence of Steps outlining the preconditions and flow of events that will take place. The first word of a step is a keyword, typically one of * Given - Describes the preconditions and initial state before the start of a test and allows for any pre-test setup that may occur * When - Describes actions taken by a user during a test * Then - Describes the outcome resulting from actions taken in the When clause Occasionally, the combination of Given-When-Then uses other keywords to define conjunctions * And - Logical and * But - Logically the same as And, but used in the negative form Scenario: A user attempts to withdraw more money than they have in their account Given John has a valid Credit or Debit card And his account balance is $20 When he inserts his card And withdraws $40 Then the ATM displays an error And returns his card But his balance remains $20


Tags

Gherkin's Feature structure forces organisation. However, in cases where this default organisation is inconvenient or insufficient, Gherkin provides Tags. Tags are @-prefixed strings and can be placed before * Feature * Scenario * Scenario Outline * Examples An element can have multiple tags and inherits from parent elements.


Cucumber


Step definitions

Steps in Gherkin's .feature files can be considered a method invocation. Before Cucumber can execute a step it must be told, via a step definition, how that step should be performed. Definitions are written in
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
and conventionally filed under features/step_definitions/*_steps.rb. Definitions start with the same keywords as their invocation (including Gherkin's full language support). Each definition takes two arguments * Either a
regular expression A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or ...
or string with $variables * A block containing ruby code to execute Example using regular expressions Given /(.*) has a valid Credit or Debit card/ do , name, # Ruby code end Example using strings and $variables. Note that at runtime the string is converted into a regular expression, and any $variable is converted to match (.*). Given "$name has a valid Credit or Debit card" do , name, # Ruby code end


Hooks

Hooks are Cucumber's way of allowing for setup to be performed prior to tests being run and teardown to be run afterwards. They are defined as executable Ruby blocks, similar to
JUnit JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated ...
methods marked with @Before, @After annotations. Conventionally they are placed under support/, and are applied globally. Three basic types of hooks exist * Before - Runs before a scenario * After - Runs after a scenario * Around - Assumes control and runs around a scenario Additional hooks include * BeforeStep * AfterStep * AfterConfiguration - Runs after Cucumber configuration and is passed an instance of the configuration Before, After, and Around hooks optionally take a list of tags filtering scenarios that they apply to. A list of tags in the same string is treated as OR, while individual arguments are treated as AND; tags can be optionally negated by being preceded with ~. Example of a tagged before hook Before('@ATM') do , scenario, # Ruby code end Hooks are often used to maintain the database state, typically by cleaning up prior to running a scenario. It is also possible to start and roll back a transaction using Before and After hooks, and many Cucumber extensions provide an @txn tag for such a purpose.


Integrations and implementations

Non Ruby implementations of Cucumber exist for popular languages including
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
,
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
, and
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
. Support also exists for integration testing frameworks. A complete list of implementations can be found on Cucumber. Cucumber has integrated testing tools working well with many Continuous Integration configurations. There are cucumber plugins for popular CI tools like Jenkins and TeamCity and also for IDEs like
Eclipse An eclipse is an astronomical event that occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ce ...
and
RubyMine JetBrains s.r.o. (formerly IntelliJ Software s.r.o.) is a Czech software development company which makes tools for software developers and project managers. , the company has offices in Prague; Munich; Berlin; Boston, Massachusetts; Amsterda ...
. Below is an example of a step definition written for Java with Cucumber-JVM. @Given("(.*) has a valid Credit or Debit card") public void has_card(String name)


Formatter plugins

Cucumber uses Formatter Plugins to provide output. Several common formats are provided by default, including *
JSON JSON (JavaScript Object Notation, pronounced ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other ser ...
*
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
*
JUnit JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated ...
Available formats are not standardized across different Cucumber implementations, so offerings may differ. Cucumber also supports rich output formats like images and videos.


Browser automation

Cucumber does not provide built-in browser automation. However, it does work well with existing programs such as
Selenium Selenium is a chemical element with the symbol Se and atomic number 34. It is a nonmetal (more rarely considered a metalloid) with properties that are intermediate between the elements above and below in the periodic table, sulfur and tellurium, ...
and WATiR-WebDriver. It does support running tests with transactions through leveraging other programs such as
ActiveRecord In software engineering, the active record pattern is an architectural pattern. It is found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book ''Patterns of Enterprise Application ...
.


Cucumber 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. $ 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 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. Cucumber can also be configured to ignore certain scenarios that have not been completed by marking them with the Work In Progress tag @wip. When Cucumber is passed the --wip argument, Cucumber ignores scenarios with the @wip tag.


References

{{reflist


External links


Engineering Software as a Service: An Agile Approach Using Cloud Computing by Armando Fox and David Patterson

Cucumber project

Cucumber project documentation

At the Forge - Cucumber
by Reuven M. Lerner in the
Linux Journal ''Linux Journal'' (''LJ'') is an American monthly technology magazine originally published by Specialized System Consultants, Inc. (SSC) in Seattle, Washington since 1994. In December 2006 the publisher changed to Belltown Media, Inc. in Houston ...

Agile 2009 - Aslak Hellesoy - Cucumber test framework
podcast by Bob Payne with Aslak Hellesøy

by Mike Gunderloy
Specflow, Cucumber in .NET
Free software testing tools Software using the MIT license Free software programmed in Ruby