Skip to main content

Features and Scenario in Behat

Feature File: In a project we have different feature files.  A file in which we store feature, feature description and scenarios to be tested. The extension of feature file should be ".feature".  For each functionality there should be separate feature file. Example search.feature, registration.feature etc.The names for Scenarios and Feature file must be unique.

In feature file you describe your test in Descriptive language like English, French etc. Feature file contains Business requirement. It is an essential part of Behat, as it serves as an automation test script as well as live documents.

Feature file can contain either Scenario or Scenario outline or combination of them. A feature file is an entry point to the Behat tests.

Feature: A feature can be defined as a standalone unit or functionality of a project. Each independent functionality of the project under test can be termed as a feature. Each feature file start with keyword Feature: or its localised equivalent.

Feature Title: The text that immediate follow feature keyword is Title of the Feature file.

Feature Description: The suggested best practice is, to write a small description of the feature beneath the feature title in the feature file. This will fulfil the need of a good documentation as well.

Scenario: A feature file can have one or more scenarios. A feature consist of list of scenarios which start with Scenario: keyword. Scenario is one of the core Gherkin Structures. Every scenario starts with the keyword Scenario (or its localized keyword). The keyword Scenario is a synonym of the keyword Example.

Scenario Title: "Scenario:" keyword is followed by an optional scenario title.

Steps: Scenario further consist of Steps which start with keyword like Given, When, Then, But or And. Every scenario has one or more steps.

When we specify business requirement, we need to specify the pre-conditions, user actions and expected output. These can be easily represented using given, when and then.

  • Given: Pre-conditions are mentioned in Given keyword.
  • When: User actions are described in When.
  • Then: It is used for expected output.

Example of Feature and Scenario:

#This is sample comment
@Search
Feature: Search
  In order to filter the content
  User should be able to
  to search on the website

  @tag1
  Scenario: Search for a word that does not exist
    Given I am on the homepage
    When I fill in "searchTerm" with "XBox"
    And I press "search_submit"
    Then I should see "No products found"

  @tag2
  Scenario Outline: Search for a product
    Given I am on the homepage
    When I fill in "searchTerm" with "<term>"
    And I press "search_submit"
    Then I should see "<result>"
    Examples:
      | term    | result              |
      | Samsung | Samsung Galaxy S II |
      | XBox    | No products found   |

In above example @search, @tag1 and @tag2 are tags. Comments are there with #. There is Background section also, which we will discuss later.

Tags
Submitted by tgoswami on September 16, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.