top of page

Cucumber Tags

What Are Tags In Cucumber ?

Cucumber tag is a way to organize the execution of scenarios in feature files.Each scenario or feature can be defined by using tags in feature file.

We can decide which specific tag(scenario or feature),we want to execute in the runner file.

Tags start with '@' and followed by a relevant text which is the name of that specific tag.

Tags can be defined at both feature level and scenario level.Tags defined at feature level ensures that all the scenarios within that feature inherits its tag.

We can use one or more tags for the single feature/scenario.

Tags are mainly of two types :

  • Default Tags - They have predefined name with predefined functionality.

  • Custom Tags - These are fully flexible user defined tags.

Here's an example of Cucumber Tags :

Step-Definition class as 'TagsExample.java' :

package stepdefinitions;

import io.cucumber.java.en.Given;

public class TagsExample {
   @Given("^Run step of scenario 1$")
   public void run_Scenario_I() throws Throwable {
       System.out.println("Runnig step of Scenario 1");
   }

    @Given("^Run step of scenario 2$")
   public void run_Scenario_II() throws Throwable {
       System.out.println("Runnig step of Scenario 2");

    }

    @Given("^Run step of scenario 3$")
   public void run_Scenario_III() throws Throwable {
       System.out.println("Runnig step of Scenario 3");

    }

    @Given("^Run step of scenario 4$")
   public void run_Scenario_IV() throws Throwable {
       System.out.println("Runnig step of Scenario 4");

    }

}
 

Feature file as 'featureTag.feature' :

Feature: Tags Example

  @RunTag
  Scenario: Scenario 1 is included
    Given Run step of scenario 1
   
  @ExecuteTag


  Scenario: Scenario 2 is included
    Given Run step of scenario 2
    
  @ExecuteTag
  Scenario: Scenario 3 is included
    Given Run step of scenario 3
    
  @RunTag 
  Scenario:  Scenario 4 is included
    Given Run step of scenario 4

 


Test Runner class as 'TestRunnerForTags' for single tag execution :

package cucumberTest;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
       features = "Features/featureTag.feature"       

       ,glue={"stepdefinitions"}                                 
       ,tags= "@RunTag"
       )

public class TestRunnerForTags {

}
 

Run as Junit Test :

Performing Logical 'OR',Logical 'AND' and Logical 'NOT'on Cucumber Tags in Test Runner Class

If logical OR is used between two or more tags that means scenarios tagged with atleast one of ORed tag would be executed.In contrary,logical AND implies that scenaio must be tagged with all of the ANDed tags otherwise it will not execute it.Wherever logical NOT complements the operation(ignore or skip).

Syntax :

  • For logical OR : tags= "@RunTag or @ExecuteTag"

  • For logical AND : tags= "@RunTag and @ExecuteTag"

  • For logical NOT : tags= "not @RunTag"

Let's understand this with an example :

  • Consider the above same step-definition file.

  • Make some changes in above feature file as :

Feature: Tags Example

  @RunTag
  Scenario: Scenario 1 is included
    Given Run step of scenario 1
   
   @RunTag @ExecuteTag
  Scenario: Scenario 2 is included
    Given Run step of scenario 2
    
  @ExecuteTag
  Scenario: Scenario 3 is included
    Given Run step of scenario 3
    
  @RunTag  @ExecuteTag
  Scenario:  Scenario 4 is included
    Given Run step of scenario 4

 

 

  • Create test runner class to demonstrate logical OR :

package cucumberTest;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
       features = "Features/featureTag.feature"
       //pass the path of feature file or the folder name that contains feature file
       ,glue={"stepdefinitions"}                                 //package name of step definitions classes
       ,tags= "@RunTag or @ExecuteTag"
       )

public class TestRunnerForTags {

}
 

Output :

 

  • Create test runner class to demonstrate logical AND :

package cucumberTest;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
       features = "Features/featureTag.feature"    
//pass the path of feature file or the folder name that contains feature file
       ,glue={"stepdefinitions"}                              //package name of step definitions classes
       ,tags= "@RunTag and @ExecuteTag"
       )

public class TestRunnerForTags {

}

Output :

  • Create test runner class to demonstrate logical NOT :

package cucumberTest;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
       features = "Features/featureTag.feature"         
 //pass the path of feature file or the folder name that contains feature file
       ,glue={"stepdefinitions"}                                      //package name of step definitions classes
       ,tags= "not @RunTag"
       )

public class TestRunnerForTags {

}

 

Output :

 

Refer next page Cucumber Hooks

singletagexample.png
logicalORexm.png
logicalANDexm.png
logicalNOTexm.png
bottom of page