1.GherkinIntroduction:
Cucumber is an interpreter. Just like the ruby code in the. rb file, Cucumber is used to execute the Gehrkin code in the. feature file.
2.Keywords:
When writing a feature file, you will use a lot of specific keywords in the Gherkin language, mainly including the following:
• Feature
• Background
• Scenario
• Scenario outline
• Scenarios (or examples)
• Given
• When
• Then
• And (or)
• | (Used to define a table)
• "" (Defining multi-line strings)
• # (Comment)
We can write anything we want to write after the keyword. The keywords Given, When, Then, And But indicate the steps in a scenario.
Each feature file must start with the keyword Feature, followed by a colon and a description. This description can be performed on many rows, but in general, a better mode is to write a brief overview, followed by a concise description of this in the next line. For example:
Feature: Book flight
In order to book a flight
As a user
I want to find the cheapest flight
Of course, Cucumber does not care about what you have written here. It simply ignores the code until it encounters the keyword Background, Scenario and Scenario Outline. We write this only for communication and communication.
3.Scenarios/Steps/The cucumber Command
1. Scenario: There can be multiple Scenario in a feature file. One Scenario is a specific functional point you want to test. A Scenario is composed of multiple Steps (Steps. Scenario clearly describes the behavior of the program we expect and facilitates communication between people in different positions. As long as you see a Scenario, it is easy to know what it wants to do.
The first part of a Scenario is the Scenario keyword followed by a colon and a description of the expected Scenario. 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 occurs in this scenario. A step is usually a separate line of text and starts with the following keywords: given, when, then, and. as follows: (Note: The following steps do not need to be followed by a colon)
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 run it using the cucumber command. If the cucumber command is not followed by anything, it will execute all. feature files. If you only want to run a. feature file, run the cucumber features \ feature_name command.
4.Given/When/Then/And/
A) Given: indicates what we accept/think is true in scenario. It will provide background/context for the following when (event/operation) and Then (output); for example: given I visit a website
B) When: indicates an event or action in scenario. For example, 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 to locate errors when they fail;
C) Then: indicates the expected output;
D) And/But: We can use And But as synonyms of Given/When/Then, such as: "Given x,AndY, "here'sAndIt means the meaning of Given; Then, for example, "Then x, But not y," But here is the meaning of Then;
Example:
Scenario: transfer money (declarative)
Given I have $100 inchecking
And I have $20 insavings
When I transfer $15 from checking to savings
Then I shoshould have $85 inchecking
And I shoshould have $35 insavings
5.Tags:
As mentioned above, a. feature file can contain many scenario components. If we run a feature file that contains many scenario files, it will execute all the scenario files in the file; however, when we only want to run some special scenario, we can use Tags;
The Tag in Cucumber looks like the Ruby instance variable. For example, @ wip, @ foo... You can add any number of tags to the Feature or Scenario before the feature or scenario keyword, for example:
@ Approved @ book_flight
Feature: Book flight
@ Wip
Scenario: Book a flight on web
A Scenario will inherit the tags assigned to the Feature. Therefore, in the above example, Scenario has three tags: @ approved @ book_flight @ wip. then we can use the command cucumber -- tags tag_name to run the part of Scenario we want to run. for example, 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 containing @ 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 does not contain @ Dev
Cucumber -- tags @ Foo ,~ @ Bar -- tags @ Baz
# (@ Foo |! @ Bar) & @ Baz
Note: This article is based on the book The rspec book. I only keep records and have a limited level. You can view the original books if necessary.