</>

Technology

Cucumber

Difficulty

Intermediate

Interview Question

How do Cucumber tags work and what is an advanced tagging strategy?

Answer

Cucumber Tags — Advanced Strategy

Basic Tagging

Gherkin
@smoke @login
Scenario: Successful login
  Given the user is on the login page
  ...

@regression @slow @payments
Scenario: Full checkout flow
  Given the user has items in cart
  ...

@skip @wip
Scenario: Feature in development
  Given this is not ready
  ...

Tag Filtering from @CucumberOptions

Java
// Run ONLY @smoke scenarios
@CucumberOptions(tags = "@smoke")

// Run @smoke AND @login (both tags required)
@CucumberOptions(tags = "@smoke and @login")

// Run @smoke OR @sanity
@CucumberOptions(tags = "@smoke or @sanity")

// Run @regression but NOT @slow
@CucumberOptions(tags = "@regression and not @slow")

// Exclude WIP scenarios
@CucumberOptions(tags = "not @wip and not @skip")

Override Tags from Maven CLI

Bash
# CI smoke run
mvn test -Dcucumber.filter.tags="@smoke"

# Full regression excluding slow
mvn test -Dcucumber.filter.tags="@regression and not @slow"

# Environment-specific
mvn test -Dcucumber.filter.tags="@staging"

# Priority-based
mvn test -Dcucumber.filter.tags="@P1 or @P2"

Enterprise Tagging Strategy

Gherkin
-- Execution tier tags
@smoke        -- ~15 tests, runs in < 5 min (every commit)
@sanity       -- ~50 tests, runs in < 20 min (every PR)
@regression   -- all tests, runs nightly

-- Priority tags
@P1           -- business-critical
@P2           -- important
@P3           -- low priority

-- Environment tags
@staging      -- staging environment only
@prod-safe    -- safe to run in production

-- State tags
@wip          -- work in progress (excluded from CI)
@skip         -- permanently excluded
@flaky        -- known flaky (run separately)

-- Module tags
@login @checkout @cart @payments @search

-- Type tags
@api @ui @db

Tagged Hooks (Scope Hooks to Tags)

Java
// Only runs before @login scenarios
@Before("@login")
public void navigateToLoginPage() {
    driver.get(config.getBaseUrl() + "/login");
}

// Only runs after @db scenarios
@After("@db")
public void cleanupDatabase() {
    dbUtils.rollbackTestData();
}

// Tag at Feature level — applies to ALL scenarios in that feature
Gherkin
@regression @payments
Feature: Payment Processing

  @smoke @P1
  Scenario: Pay with valid credit card
    ...

  @P2
  Scenario: Pay with expired card
    ...

The @regression @payments tags apply to both scenarios. @smoke @P1 applies only to the first.

Multiple TestRunners for Tag Sets

Java
// SmokeTestRunner.java
@CucumberOptions(tags = "@smoke", plugin = {"html:target/smoke-report.html"})
public class SmokeTestRunner extends AbstractTestNGCucumberTests {}

// RegressionTestRunner.java
@CucumberOptions(tags = "@regression and not @wip", plugin = {"html:target/regression-report.html"})
public class RegressionTestRunner extends AbstractTestNGCucumberTests {}

Follow AutomateQA

Related Topics