3. Cucumber features

“Feature” in cucumber is supposed to describe a single feature which is analog with “test group”/”test class” in standard Java world. A feature includes one or more scenarios. A scenario can be represented as a “test case” or a ”test method“ in standard Java world. The code of a cucumber feature need to be placed in a file with “.feature” extension and must be located in a separated folder on the project root.
Creating a new feature file with the previously installed eclipse extension will include a small legend and a simple template for the feature file.

Most important Gherkin keywords are:

  • Feature – define the name of the current feature
  • Scenario – begins a new scenario and define its name
  • Given – precondition step
  • When – key steps of the scenario
  • Then – validations and expected outcome of the key steps
  • And, But – allow additional steps for the previous keywords
  • The detailed description of gherkin can be found here.

The following example is demonstrating a typical feature file:
Feature: Sign up
Sign up should be quick and friendly.
Scenario: Successful sign up
New users should get a confirmation email and be greeted
personally by the site once signed in.
Given I have chosen to sign up
When I sign up with valid details
Then I should receive a confirmation email
And I should see a personalized greeting message
Scenario: Duplicate email
Where someone tries to create an account for an email address
that already exists.
Given I have chosen to sign up
But I enter an email address that has already registered
Then I should be told that the email is already registered
And I should be offered the option to recover my password

This Feature includes two scenarios, each with 4 steps.
The text between the feature name and the first scenario (Sign up should be quick and friendly.) and the sentences between the scenario name and the first test step are just descriptions.

The Given-When-Then trio is a common way to separate precondition, test steps and expected results. If one of those needs extra steps, it can be added by using prefix And or But.

The execution makes no difference between these three parts, it is only a logical separation for easier understanding of the test cases. Instead of these 5 keywords we can use joker character * and the test result will be the same, but the test case readability will deteriorate. This can be seen on the empty test run as well:

Test case execution will be explained in the future chapters along with more complex Cucumber test case structures.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>