Skip to main content

Tags

Gherkin tags (@smoke, @regression, …) map to JUnit @Tag annotations for selective test execution.

Source: examples/getting-started/example-7 on GitHub.

What this demonstrates​

  • Feature-level tags → @Tag on the generated outer test class
  • Rule-level tags → @Tag on the @Nested rule class
  • Scenario-level tags → @Tag on the @Test method
  • Multiple tags → wrapped in a @Tags container annotation
  • Tags enable filtering in IDEs, Maven Surefire, and CI pipelines
  • Tags carry over to the concrete subclass that implements the step methods — it declares no tags of its own, yet filtering selects its tests exactly as tagged in the spec

Gherkin → JUnit mapping​

GherkinJUnit
@cart @regression on a Feature@Tags({@Tag("cart"), @Tag("regression")}) on outer class
@ui @shipping on a Rule@Tags({@Tag("ui"), @Tag("shipping")}) on @Nested class
@smoke on a Scenario@Tag("smoke") on @Test method
@smoke @happy-path on a Scenario@Tags({@Tag("smoke"), @Tag("happy-path")}) on @Test method

Tags and the concrete test class​

ShoppingCartTest extends the generated ShoppingCartScenarios and implements its step methods — and needs no tags of its own. JUnit's @Tag is @Inherited, so the feature-level tags come with the generated class, while the rule and scenario tags stay on the inherited @Nested classes and @Test methods. Tag filtering selects tests from ShoppingCartTest as is.

ShoppingCartFeature.java          (marker class, @Gherkin2JUnit)
└→ ShoppingCartScenarios.java (generated, abstract, carries the @Tag annotations)
└→ ShoppingCartTest.java (your concrete class, implements step methods, inherits the tags)

Running filtered tests​

IntelliJ IDEA — right-click a tag in the test class → Run tests tagged…

Maven Surefire:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>smoke</groups> <!-- include only @smoke -->
<excludedGroups>edge-case</excludedGroups> <!-- exclude @edge-case -->
</configuration>
</plugin>

Gradle — useJUnitPlatform { includeTags 'smoke' }.

Run it​

cd examples/getting-started/example-7
mvn test