Q8 of 21 · BDD / Cucumber

How do tags work in Cucumber and how do you run only specific tagged scenarios?

BDD / CucumberJuniorbddcucumbertagsfilteringci

Short answer

Short answer: Tags are @-prefixed labels placed above Feature or Scenario keywords. They control which scenarios run via tag expressions in the CLI or @CucumberOptions, and can trigger hooks that run only for matching tags.

Detail

@smoke @login
Feature: User login

  @positive
  Scenario: Successful login
    ...

  @negative @wip
  Scenario: Login with wrong password
    ...

Tags on a Feature apply to all its scenarios. Tags on a Scenario apply only to that scenario.

Running by tag — CLI:

# Run all @smoke scenarios
cucumber --tags @smoke

# Run @smoke but not @wip
cucumber --tags "@smoke and not @wip"

# Run @smoke OR @regression
cucumber --tags "@smoke or @regression"

Java @CucumberOptions:

@CucumberOptions(tags = "@smoke and not @wip")

Common tagging strategies:

  • @smoke — fast, critical-path tests run on every PR
  • @regression — full suite run on scheduled pipeline
  • @wip — work in progress; excluded from CI
  • @slow — separated to a nightly job
  • @api / @ui — split suites by layer
  • @JIRA-1234 — link scenarios to tickets (useful for traceability)

Tagging hooks — run setup only for specific tags:

@Before("@db")
public void setupDatabase() { /* runs only for @db tagged scenarios */ }

// WHAT INTERVIEWERS LOOK FOR

Tag expression syntax (and, or, not). Practical tagging strategies for CI gating. Tagged hooks as a bonus.