Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Gherkin keywords

In Gherkin, every non-empty line starts with a Gherkin keyword (although there are several common exceptions). We will go over the relevant keywords in more detail when we use them, but here's a brief overview of each keyword and its uses:

  • Feature: Specifies the name and description of the feature. A feature is just a way to group related scenarios together.
  • Scenario: Specifies the name and description of the scenario.
  • Given, When, Then, And, But: Each scenario is made up of one or more steps, each corresponding to a JavaScript function that is to be executed by Cucumber. If, after executing all the steps, no errors were thrown, then the test is deemed to have passed. The five step keywords are equivalent; you should use the one that makes your test most readable.
  • Background: Allows you to set up a common environment to execute all your scenarios. This saves you from defining duplicate set-up steps for all scenarios.
  • Scenario Outline: Allows you to define a template for multiple scenarios that differ only in certain values. This prevents specifying many scenarios/steps that are very similar to each other.
  • Examples: When using scenario outline, the Examples keyword allows you to specify values to plug into the scenario outline.
  • """: Allows you to use doc strings to specify multiline strings as parameters.
  • |: Allows you to specify more complex data tables as parameters.
  • @: Allows you to group related scenarios together using tags. After tagging scenarios, you can instruct Cucumber to execute only those with a certain tag, or, conversely, exclude tests with certain tags.
  • #: Allows you to specify comments, which will not be executed by Cucumber.
If you are using Visual Studio Code (VSCode), we recommend that you install the Cucumber (Gherkin) Full Support VSCode Extension ( github.com/alexkrechik/VSCucumberAutoComplete), which provides syntax highlighting and snippet support.