Skip to main content

Tags in Cucumber

We have generally hundreds to thousands of test cases in our suite. We don't run all the test cases at the same time, we categorized them according to feature, smoke test cases, Regression test cases or End to End test cases etc.

If we have made changes to particular functionality and now we want to test that particular functionality then we can use tags.

In Cucumber, to organize our test cases we use tags in feature file. Tags can be for complete feature file or for an individual test case or combination of test cases. Tagged scenario can be executed using Cucumber JUnit Runner class. Tags starts with @ and you can write any text after that without giving space. Example @smoke, @regression, @search etc.

Single scenario or feature file can have multiple tags which you can call based on your need. There is no limit in defining tags within feature file.

There are mainly 2 types of tags:

1. Default tag: Default tag has their predefined meanings. Example @Dev, @Ignore etc.
2. Custom tag: You can define of your own.

Consider below example:

@userflow
Feature: Registration, Login and MyAccount

  Background:
    Given I am on the homepage
    And I follow "Sign in"
  
  @regression
  Scenario Outline: Verify Login Functionality
    And I fill "email address textbox" with "<email>"
    And I fill "password textbox" with "<password>"
    And I click "sign in button"
    Then I should see "<heading>" heading

    Examples: 
      | email                     | password | heading        |
      | goswami.tarun77@gmail.com | test1234 | MY ACCOUNT     |
      | wrongusername             | test     | AUTHENTICATION |

  @regression @smoke
  Scenario: Create New User
    And I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"
    And I enter following details
      | First Name | Tarun    |
      | Last Name  | Goswami  |
      | Password   | Test1234 |
      | Date       |       13 |
      | Year       |     1989 |
    And I click "register button"

In above file feature file tagged with @userflow, both scenario tagged with @regression and one with @smoke.

This is how you can call above tags using JUnit Runner Class:

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
		plugin = {"pretty", "html:target/cucumber"},
		features = {"features"},
		glue={"com.pb.cucumbertest.stepdefinitions"},
		tags = {"@login"}
		)

public class Runner {

}

You can also skip your custom tag using the ~ operator in Cucumber Options. We can also use logical operators with tag.

Be aware that tags are inheritable within Feature files:

1. Scenarios inherit tags from the Feature statement. That means if you call feature file tag @userflow, all scenario(both in above example) with in it will be executed except the skipped ones.
2. Scenario Outline "Examples" inherit tags from the Feature and Scenario statements. In above example, "Create New User" Scenario can be called with 3 tags @userflow, @regression and @smoke.

Submitted by tgoswami on August 30, 2020

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.