Writing gherkin test cases in cucumber

From Test Automation Wiki
Revision as of 12:38, 10 October 2018 by Jvanzanten (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test case structure

In gherkin test cases are build up in the following way.

Project

A project collection of features. A project can contain:

  • Front End & Back End
  • Multiple languages
  • A combination of websites that are (very) similar

Feature

A feature is where you describe a major part of your project. There is no wrong or right way to split your features, the point is to make it in a way that makes it organized.

In an optimal world, you would like to have a maximum of 10 scenario's in one feature. If it becomes more, you should look into splitting it up into more scenario's.

Keep in mind that you create extra folders in the /feaure/ folder. For example: /feature/user/registration.feature. More info about keeping your project organized

An example of features for a website:

  • navigation.feature
  • user_account.feature
  • shopping_basket.feature
  • checkout.feature

Scenario

A scenario is a part of a feature which contains the description of a specific functionality. For example if the login button works or if the `invalid credentials` message works.

As a best practice you should confirm 1 functionality per scenario. More information see gherkin best practice.

Step

A step is used to describe a part of the scenario.

Feature contents

Below there is an example of a feature file.

Error creating thumbnail: Unable to save thumbnail to destination

The document starts off with Feature  : following with the title of the feature . It's a common rule to have the same file name as the feature title.

After the title, the description follows. This is a free text starting on a new line. At this part you can make a small summary of what the feature file contains.

After this you can follow with one or more Scenarios with a name .

Scenario contents

Error creating thumbnail: Unable to save thumbnail to destination

The texts " Given, And, When, Then " are all used to make it readable. However, Cucumber does expect one of these words before every step to recognize the beginning of a new step .

Scenario outline contents

Sometime you want to use different data for the same scenario. This can be done with a so called Scenario Outline.

Error creating thumbnail: Unable to save thumbnail to destination

You can create a scenario outline the same way as a regular scenario, write Scenario Outline: followed by the name of the scenario.

After writing the steps for the scenario you can write Scenarios: followed by a new line.

Now you can create a grid/table, by using the symbol |. The first row of this table will represent the variable names . Every following row will be the variable value for that specific scenario. (Result in the first scenario is 120 and in the second it is 20).

Now you can use the variable value in your steps by using <variablename>. So in this example, the 4th step in the 2nd run will be:

Then the result should be 20 on the screen.

Tags

Tags allow you to group or individually select scenarios or features for running. We will be actually running the features in the next article.

@calculator
Feature: Calculation
  I want to calculate something

  @addition
  Scenario: Add two numbers
    Given I have entered 50 in the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen

  @subtraction
  Scenario: Subtract two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press subtract
    Then the result should be -20 on the screen

Running with the @calculator tag executes the entire feature.

Running with either of the @addition or @subtraction tag only executes one scenario each.

You can also run with @calculator ~@subtraction to deselect subtraction tests

Adding a feature file to the project

In to execute this code yourself it first needs to be added to the project.

To keep our files clean we want to add it besides the other .feature files.

Creating a new file can be done in a text editor or go directly to the folder and create a new text document.

Lets name the file "calculator.feature". We use the extension ".feature" so cucumber knows that it should look there for executable scenario's.

Once the file is created, open it and copy the above text into it. don't forget to save