Tags in Cucumber

  

Tags in Cucumber are used to filter scenarios or features for execution. They are written using the @ symbol and help organize or group test cases.


You can:

  • Run only specific scenarios.

  • Skip specific scenarios.

  • Group scenarios for smoke, regression, sanity testing, etc.



Why Use Tags?

  • To control which scenarios to run.

  • To categorize tests like @smoke@regression@login, etc.

  • To exclude certain tests during a test run.



Example Feature File with Tags (Login.feature)

@regression
Feature: Login functionality

  @smoke @login
  Scenario: Successful login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be redirected to the home page

  @negative
  Scenario: Login with invalid credentials
    Given the user is on the login page
    When the user enters invalid credentials
    Then an error message should be displayed


How to Run Scenarios with Tags?

When using JUnit with Cucumber, configure the @CucumberOptions in the test runner class.



Test Runner Class Example:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepdefinitions",
    tags = "@smoke",
    plugin = {"pretty", "html:target/cucumber-reports"}
)
public class TestRunner {
}


tags Options:

ExpressionDescription
@smokeRuns scenarios with @smoke tag
@smoke and @loginRuns scenarios with both tags
@smoke or @regressionRuns scenarios with either tag
not @negativeExcludes scenarios with @negative tag


Important Points:

  • Tags can be placed above Feature or Scenario.

  • Tags can be combined using logical operators (andornot).

  • Cucumber treats tags case-sensitive.