Cucumber Introduction to the Gherkin

Source: Internet
Author: User

1.Gherkin Introduction:

Cucumber is an interpreter, just as the ruby command executes the Ruby code in an explanation. rb file, cucumber is used to perform an explanation of the Gehrkin code in the. feature file.

2. Keywords:

When you write a feature file, you will use a number of specific keywords in the Gherkin language, mainly including the following:

feature

background

Scenario

Scenario Outline

scenarios (or examples)

given

when

Then

and (or But)

| (Used to define the table)

• "" (Define multi-line string)

# (Note)

We can write anything we want to write behind the keyword. The keywords given, when, then, and and are used to indicate steps in a scene.

Each feature file must start with the keyword feature, followed by a colon and a description. This description can be on many lines, but generally the better pattern is to write a short summary, followed by a concise description on the next line. Like what:

Feature:book Flight

In order to book a flight

As a user

I want to find the cheapest flight

One thing to point out, of course, is that cucumber doesn't care what you write here. It simply ignores the code until it encounters the keyword background, Scenario, and Scenario Outline. The purpose of our writing is simply to communicate and to communicate.

3. scenarios/steps/the Cucumber Command

1. Scenario: A feature file can have a lot of Scenario, a Scenario is a specific point you want to test the function, a Scenario is a lot of steps (Steps) composition. Scenario clearly describes the behavior of the program we expect and facilitates communication between people in different positions because you can easily see what it wants to do as long as you have a scenario.

The first part of a scenario is the Scenario keyword followed by a colon and a description of the supposed scene. As follows:

Feature:book Flight

In order to book a flight

As a user

I want to find the cheapest flight

Scenario:successful Booking Flight

2. Steps: Each scenario can use any number of steps to describe anything that happens in the scene, a step is usually a single line of text and begins with these keywords: Given,when,then,and and But. As follows: (Note: No colon is required after the step)

Feature:book Flight

In order to book a flight

As a user

I want to find the cheapest flight

Scenario:successful Booking Flight

Given I visit a website to book flight

3. The cucumber command: Once we have written a feature file, we can use the cucumber command to run it. If the cucumber command does not follow anything, then it will execute all the. feature files. If we only want to run a certain. feature file, we can use the command cucumber features\feature_name

4.given/when/then/and/but

A) Given: denotes what we accept/consider to be true in scenario, which will provide a background/context for the subsequent when (event/operation) and then (output), e.g. Given I visit a website

b) When: Indicates an event or action in the scenario, e.g. when I click the Submit button.; Generally we like to have only one event or action in a scenario, because it is easy to understand the purpose of scenario and easy to find mistakes when they fail;

c) Then: indicates the expected output;

D) And/but: We can use and and but as a synonym for the given/when/then, such as: "Given x, and Y," here and is the meaning of Given, again such as: "Then x, but not y," here's but th The meaning of en;

Cases:

Scenario:transfer Money (declarative)

Given I have $ inchecking

And I have $ insavings

When I transfer $ from checking to savings

Then I should has $85 inchecking

And I should have $ insavings

5. Tags:

As mentioned earlier, a. feature file can have many scenario components. If we run a feature file that contains a lot of scenario, it will execute all the scenario in the file, but sometimes we may just want to run one or some special scenario when we can use tags;

In cucumber, the tag looks like a ruby instance variable. such as @wip, @foo ... You can add any number of tags to feature or scenario before the feature or scenario keyword, such as:

@approved @book_flight

Feature:book Flight

@wip

Scenario:book a flight on web

A scenario will inherit the tags assigned to feature, so in the example above, scenario has three tags: @approved @book_flight @wip. Then we can use the command: Cucumber--tags tag_name To run the part of the scenario that we want to run. such as: Cucumber–tags @wip

In addition,--tags supports complex representations, including: And,or and not, as follows:

Cucumber–tags @foo, @bar

# @foo | | @bar Run all scenarios that contain @foo OR @bar

Cucumber--tags @foo--tags @bar

# @foo && @bar Run all scenarios containing @foo and @bar

Cucumber--tags ~ @dev

#! @dev Run all scenario that do not contain @dev

Cucumber--tags @foo, ~ @bar--tags @baz

# (@foo | |! @bar) && @baz

Note: The content of this article is from the book RSpec

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.