Skip to main content

Behat: Tags

We have generally hundreds to thousands of test cases in our suite. We don't run all the test cases at the same time, we categorized them according to feature, smoke test cases, Regression test cases or End to End test cases etc. We even can have environment specific tags like @dev for development environment, @stg for staging and @prd or @prod for production.

If you have made changes to particular functionality and now you want to test that particular feature or scenario then you can use tags.

In Behat, to organize our test cases we use tags in feature file. Tags can be for complete feature file or for an individual test scenario or combination of scenarios. Tagged scenario can be executed using Behat Command Line. Tags starts with @ and you can write any text after that without giving space. Example @smoke, @regression, @search etc.

Single scenario or feature file can have multiple tags which you can call based on your need. There is no limit in defining tags within feature file, you just need to separate tags with space.

Consider below example:

#language: en
Feature: Login Functionality

    Given I am on homepage
    When I follow "Sign in"
    And I wait 3 seconds

  Scenario: Verify user Login
    And I fill in "email" with ""
    And I fill in "passwd" with "Test1234"
    And I press "SubmitLogin"
    And I wait 10 seconds

  @regression @dev
  Scenario Outline: Verify user Login for multiple users
    And I fill in "email" with "<username>"
    And I fill in "passwd" with "<password>"
    And I press "SubmitLogin"
    And I wait 10 seconds
      | username                    | password |
      | | Test1234 |
      |   | Test1234 |

In above file feature file tagged with @login, scenario tagged with @smoke and scenario outline is tagged with @regression.

This is how you can call above tags using Behat Command Line:

$ bin/behat --tags @smoke

This command will call scenario or feature file tagged with smoke. You can use even and, or and not operator with tags which we will discuss in upcoming lectures.

Be aware that tags are inheritable within Feature files:

1. Scenarios inherit tags from the Feature statement. That means if you call feature file tag @login, all scenario (scenario and scenario outline in above example) with in it will be executed except the skipped ones.
2. Scenario Outline "Examples" inherit tags from the Feature and Scenario Outline statements. In above example, "Verify user Login for multiple users" Scenario Examples can be executed with 3 tags @login, @regression or @dev.

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.


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.