Tags in Cucumber Framework

Tags in Cucumber are used to organize, filter, and control the execution of test scenarios and features.

They help run only selected tests like:

  • Smoke tests
  • Regression tests
  • Priority test cases

Where Can Tags Be Used?

Tags can be applied to the following Gherkin elements:

  • Feature
  • Scenario
  • Scenario Outline
  • Examples (in Scenario Outline)

Tag Syntax

Tags must start with the @ symbol
Example: @smoke, @regression, @critical

Example: Tags in Feature file

@mustrun
Feature: To test withdrawal, deposit, and transaction features
  @Priority
  Scenario Outline: To verify the Add Customer functionality
    Given user navigate the url in the browser
    When user select the manager login
    And add the customer with "", "", ""
    Then verify the success message with customer Id
    Then verify the added customer displayed in Customers Tab
    @smoke
    Examples:
      | username | lastname | postcode |
      | Tester   | smoke    | 32334    |
      | Develop  | smoke    | 32334    |
    @regression
    Examples:
      | username | lastname  | postcode |
      | Designer | regression| 32334    |
  @smoke
  Scenario: Add the customer and deposit the amount
    Given user navigate the url in the browser
    When user select the manager login
    And add the customer with "username", "lastname", "postcode"
    And existing customer loggedIn
    And deposit the amount "50"
  @regression
  Scenario: Verify user selects the deposit amount and generates a report
    Given user navigate the url in the browser
    When user select the manager login
    And add the customer with "username", "lastname", "postcode"
    And existing customer loggedIn
    Then download the report


Note: In Scenario Outline, we can use tags with different examples

Using Tags in Runner Class

In the Runner class, specify tags inside the @CucumberOptions annotation:

package test.utils;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
    plugin = {
        "pretty",
        "html:target/Reports/cucumber.html",
        "json:target/Reports/cucumber.json"
    },
    glue = "StepDefinitions",
    features = "src/main/resources",
    tags = "@smoke or @regression",
    monochrome = true
)
public class TestngRunner extends AbstractTestNGCucumberTests {
}


Running Tags from Command Line (Maven)

To run tests using tags from the terminal, use:

mvn test -Dcucumber.filter.tags="@smoke and @fast"
 

Related Tutorials