Girish Mahajan (Editor)

Cucumber (software)

Updated on
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Covid-19
Stable release  1.3.15 / May 9, 2014
Operating system  Cross-platform
Written in  Ruby
License  MIT License
Developer(s)  Aslak Helles√ły, Joseph Wilk, Matt Wynne, Gregory Hnatiuk, Mike Sassak
Type  Behavior driven development framework / Test tool

Cucumber is a software tool used by computer programmers for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style. Central to the Cucumber BDD approach is its plain 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. Capybara, which is a part of the Cucumber testing framework, is an example of a web based test automation software.

Contents

Cucumber is written in the Ruby programming language. 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. For example, Cuke4php and Cuke4Lua are software bridges that enable testing of PHP and Lua projects, respectively. Other implementations may simply leverage the Gherkin 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

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. # 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 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

    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.

    In some cases, one might want to test multiple scenarios at once to perform Equivalence partitioning and Boundary-value analysis. A Scenario Outline provides a technique to specify multiple examples to test against a template scenario by using placeholders. For example,

    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
  • Tags

    Gherkin's Feature structure forces organization. However, in cases where this default organization 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.

    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 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 or string with $variables
  • A block containing ruby code to execute
  • Example using regular expressions

    Example using strings and $variables. Note that at runtime the string is converted into a regular expression, and any $variable is converted to match (.*).

    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 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

    Hooks are often used to maintain 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, JavaScript, and Python. 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 and RubyMine.

    Below is an example of a step definition written for Java with Cucumber-JVM.

    Formatter Plugins

    Cucumber uses Formatter Plugins to provide output. Several common formats are provided by default, including

  • JSON
  • HTML
  • JUnit
  • 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 gems such as Selenium and WATiR-WebDriver. It does support running tests with transactions through leveraging other gems such as ActiveRecord.

    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 command line can be used to quickly run defined tests. It also supports running a subset of scenarios by filtering tags.

    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.

    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.

    More features on command line can be found here.

    References

    Cucumber (software) Wikipedia


    Similar Topics
    No God, No Master
    Dennis J Murray
    Susan Fiske
    Topics
     
    B
    i
    Link
    H2
    L