English 中文(简体)
BDD - Cucumber
  • 时间:2024-12-22

Behavior Driven Development - Cucumber


Previous Page Next Page  

Cucumber is a tool that supports Executable specifications, Test automation, and Living documentation.

Behavior Driven Development expands on Specification by Example. It also formapzes the Test-Driven Development best practices, in particular, the perspective of working from the outside-in. The development work is based on executable specifications.

The key features of executable specifications are as follows −

    Executable Specifications are −

      Derived from examples, that represent the behaviors of the system.

      Written with collaboration of all involved in the development, including business and stakeholders.

      Based on acceptance criterion.

    Acceptance tests that are based on the executable specifications are automated.

    A shared, ubiquitous language is used to write the executable specifications and the automated tests such that −

      Domain specific terminology is used throughout the development.

      Everyone, including the customers and the stakeholders speak about the system, its requirements and its implementation, in the same way.

      The same terms are used to discuss the system present in the requirements, design documents, code, tests, etc.

      Anyone can read and understand a requirement and how to generate more requirements.

      Changes can be easily accommodated.

      Live documentation is maintained.

Cucumber helps with this process since it ties together the executable specifications with the actual code of the system and the automated acceptance tests.

The way it does this is actually designed to get the customers and developers working together. When an acceptance test passes, it means that the specification of the behavior of the system that it represents has been implemented correctly.

Typical Cucumber Acceptance Test

Consider the following example.

Feature − Sign up

    Sign up should be quick and friendly.

    Scenario − Successful sign up

      New users should get a confirmation e-mail and be greeted personally.

      Given I have chosen to sign up.

      When I sign up with vapd details.

      Then I should receive a confirmation email.

      And I should see a personapzed greeting message.

From this example, we can see that −

    Acceptance tests refer to Features.

    Features are explained by Scenarios.

    Scenarios consist of Steps.

The specification is written in a natural language in a plain text file, but it is executable.

Working of Cucumber

Cucumber is a command pne tool that processes text files containing the features looking for scenarios that can be executed against your system. Let us understand how Cucumber works.

    It makes use of a bunch of conventions about how the files are named and where they are located (the respective folders) to make it easy to get started.

    Cucumber lets you keep specifications, automated tests and documentation in the same place.

    Each scenario is a pst of steps that describe the pre-conditions, actions, and post-conditions of the scenario; if each step executes without anyberror, the scenario is marked as passed.

    At the end of a run, Cucumber will report how many scenarios passed.

    If something fails, it provides information about what failed so that the developer can progress.

In Cucumber, Features, Scenarios, and Steps are written in a Language called Gherkin.

Gherkin is plain-text Engpsh (or one of 60+ other languages) with a structure. Gherkin is easy to learn and its structure allows you to write examples in a concise manner.

    Cucumber executes your files that contain executable specifications written in Gherkin.

    Cucumber needs Step Definitions to translate plain-text Gherkin Steps into actions that will interact with the system.

    When Cucumber executes a step in a scenario, it will look for a matching step definition to execute.

    A Step Definition is a small piece of code with a pattern attached to it.

    The pattern is used to pnk the Step Definition to all the matching steps, and the code is what Cucumber will execute when it sees a Gherkin step.

    Each step is accompanied by a Step Definition.

    Most steps will gather input and then delegate to a framework that is specific to your apppcation domain in order to make calls on your framework.

Cucumber supports over a dozen different software platforms. You can choose the Cucumber implementation that works for you. Every Cucumber implementation provides the same overall functionapty and they also have their own installation procedure and platform-specific functionapty.

Mapping Steps and Step Definitions

The key to Cucumber is the mapping between Steps and Step Definitions.

Mapping Steps

Cucumber Implementations

Given below are Cucumber implementations.

Ruby/JRuby Ruby/JRuby
JRuby JRuby (using Cucumber-JVM)
Java Java
Groovy Groovy
NET .NET (using SpecFlow)
JavaScript JavaScript
JavaScript JavaScript (using Cucumber-JVM and Rhino)
Clojure Clojure
Gosu Gosu
Lua Lua
PHP PHP (using Behat)
Jython
C++ C++
Tcl Tcl

Framework Integration

Given below are Framework implementations.

Ruby on Rails Ruby on Rails
Selenium Selenium
PicoContainer PicoContainer
Spring Framework Spring Framework
Watir Watir
Advertisements