Writing gherkin test cases in cucumber

From Test Automation Wiki
Revision as of 10:35, 23 June 2016 by Gijsp (talk | contribs)
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 into, 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 <yellow>Feature</yellow>  : following with the <turquoise>title of the feature</turquoise> . It's a common rule to have the same file name as the feature title.

After the title, the <gray>description</gray> 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 <green>one or more Scenarios</green> with a <pink>name</pink> .

Scenario contents

Error creating thumbnail: Unable to save thumbnail to destination

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

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 <yellow>scenario outline</yellow> 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 <gray>Scenarios:</gray> followed by a new line.

Now you can create a grid/table, by using |. The first row of this table will represent the <turquoise>variable names</turquoise> . Every following row will be the <green>variable value</green> 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.

Error creating thumbnail: Unable to save thumbnail to destination

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 de-select subtraction tests