Tags in Cucumber

a couple of years ago

A feature file can have multiple scenarios and in a real-time project you might end up creating hundreds of test scenarios. Every scenario can have different pre-requisite.

Also, not always we run all the test cases say for example if there is a small change (maybe a cosmetic change) then we do a sanity test or in some other scenario, you want to run a regression test and so on.

To take care of the above requirements cucumber provides tags and hooks.

In this tutorial, we will learn tags in cucumber.

What are tags in cucumber?

Each scenario in a feature file is assigned with a tag. These tags in feature file start with a @ symbol. Example – @SanityTest, @RegressionTest.

Then in the CucumberOptions we mention which tags to execute.

See the example below:

Here, we have three test scenarios, the first test scenario is assigned with the Sanity Test and RegressionTest,

the second scenario is assigned with only SanityTest and the third test scenario is assigned with only RegressionTest.

Feature: Test login and signup on facebook

  @SanityTest @RegressionTest
  Scenario: User login with correct credentials
    Given user navigates to facebook website
    Then user enters the emailId as saurabh.d2106@gmail.com
    Then user enters the password as abc@123
    Then user clicks on login button
    And user logged in successfully
    And user closes the browser

  @SanityTest
  Scenario: signup to facebook
    Given user navigates to facebook website
    Then user enters following details
      | firstname | saurabh                 |
      | lastname  | Dhingra                 |
      | email     | saurabh.d2106@gmail.com |
      | password  | ghs@123                 |
    Then user clicks on signup button
    And user is signed up successfully
    And user closes the browser

  @RegressionTest
  Scenario: Reset the password
    Given user navigates to facebook website
    Then user clicks on Reset password
    Then user resets the password

In CucumberOptions you can mention which tags to run.

package runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/features", glue = { "steps" }, tags = { "@SanityTest" })
public class TestRunner {

}

With the above Runner file, it will only run Sanity Tests.

To run test scenarios which have either SanityTest tag or Regression test, define the tags “,” (comma) separated.

package runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/features", glue = { "steps" }, tags = { "@SanityTest,
                                          @RegressionTest" })
public class TestRunner {

}

To run test scenarios which have both SanityTest tag and RegressionTestTag

package runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/features", glue = { "steps" }, tags = { "@SanityTest", "@RegressionTest" })
public class TestRunner {

}

To ignore test scenarios of a particular type, we can use “~” before a tag, say for example if you want to run SanityTests but not Regression pass the tags as below.

package runner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/features", glue = { "steps" }, tags = { "@SanityTest", "~@RegressionTest" })
public class TestRunner {

}

PS: For any questions, queries or feedback. Feel free to write us at saurabh@qatechhub.com or support@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: