Cucumber in a nutshell

Cucumber executes tests specified written in language called Gherkin. It is a plaint-text natural language (for example, English or one of other 60+ languages supported by Cucumber) with a given structure. Gherkin has been designed to be used by non-programmers, typically customers, business analysis, managers, and so on.

The extension for Gherkin files is .feature.

In a Gherkin file, non-blank lines can start with a keyword, followed by text in natural language. The main keywords are the following:

  • Feature: High-level description of the software feature to be tested. It can be seen as a use case description.
  • Scenario: Concrete example that illustrates a business rule. Scenarios follow the same pattern:
    • Describe initial context.
    • Describe an event.
    • Describe the expected outcome.

These actions are known in the Gherkin jargon as steps, which are mainly Given, When, or Then:

There are two additional steps: And (used for logical and for different steps) and But (used in for negative form of And).
  • Given: Preconditions and initial state before the start of a test.
  • When: Actions taken by a user during a test.
  • Then: Outcome from actions taken in the When clause.
  • Background: To avoid repeat steps in different scenarios, the keyword background allows to declared these steps, which are reused in subsequent scenarios.
  • Scenario Outline: Scenarios in which steps are marked with variables (using the symbols < and >).
  • Examples: A scenario outline declaration is always followed by one or more examples sections, which is a container table with values for the declared variables in the Scenario Outline.
When one line does not start with a keyword, that line is not interpreted by Cucumber. It is used to custom description.

Once we defined our features to be tested we need what it is called steps definition, which allows to translate plain text Gherkin into actions that actually exercise our SUT. In Java, it can be easily done by annotations to annotate methods for the step implementation: @Given, @Then, @When, @And, and @But. The string value of each step can contain regular expression which are mapped as fields in the method. See an example in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.75