Skip to main content

Cucumber Tagging Rules

In the Cucumber Rule Keyword article, we discussed how we could use Rule to group several scenarios that belong to the same business rule.

In this tutorial, we will discuss how we can execute scenarios based on rule tags. Consider this feature file:

@Feature1
Feature: Feature 1

  Background:
    Given I am on the homepage

  @Rule1
    Rule: This is Rule 1
      - Description Line 1
      - Description Line 2
  
      @Smoke
      Scenario: Scenario 1
        When I click on login link
        Then I should see "Registration" link

      @Regression
      Scenario: Scenario 2
        When I click on login link
        Then I should see "Forgot Password" link

    @Rule2
    Rule: This is Rule 1
      - Description Line 1
      - Description Line 2
  	  
      @Smoke
      Scenario: Scenario 3
        When I click on Ask Doubt link
        Then I should see "Ask Doubt" Heading

In the above example, executing the @Rule1 Tag will run Scenario 1 and 2. And, if you will execute the @Rule2 Tag, it will execute scenario 2.

Also, you can observe in output that Scenario inherited tags from features and Rule.

@Feature1 @Rule1 @Smoke
Scenario: Scenario 1                    # features/first.feature:14
  Given I am on the homepage            # com.pb.cucumbertest.stepdefinitions.FirstSD.homepage1()
  When I click on login link            # com.pb.cucumbertest.stepdefinitions.FirstSD.i_click_on_login_link()
  Then I should see "Registration" link # com.pb.cucumbertest.util.CommonSD.i_should_see_link(java.lang.String)

@Feature1 @Rule1 @Regression
Scenario: Scenario 2                       # features/first.feature:19
  Given I am on the homepage               # com.pb.cucumbertest.stepdefinitions.FirstSD.homepage1()
  When I click on login link               # com.pb.cucumbertest.stepdefinitions.FirstSD.i_click_on_login_link()
  Then I should see "Forgot Password" link # com.pb.cucumbertest.util.CommonSD.i_should_see_link(java.lang.String)

@Feature1 @Rule2 @Smoke
Scenario: Scenario 3                    # features/first.feature:29
  Given I am on the homepage            # com.pb.cucumbertest.stepdefinitions.FirstSD.homepage1()
  When I click on Ask Doubt link        # com.pb.cucumbertest.stepdefinitions.FirstSD.i_click_on_ask_doubt_link()
  Then I should see "Ask Doubt" Heading # com.pb.cucumbertest.stepdefinitions.FirstSD.i_should_see_heading(java.lang.String)

Feature tag is common in all scenario, Scenario 1 and 2 inherited @Rule1 and Scenario 3 inherited tag from Rule 2.

Note: Although this Tag is logically associated with the Rule, if you use the same Tag for any other feature file or Rule or Scenario, that feature file or Scenario linked with the same Tag will also be executed. 

Video Tutorial: Tagging Rules in Cucumber

Submitted by tgoswami on April 13, 2022

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.