Enroll in Selenium Training

So far we are good just because we have only a few couple of scenarios in the feature file. However, when you work on any real-life project, you would have many scenarios in one feature file and there will be many other feature files full of scenarios. As the name suggests Feature, we tend to create a separate feature file for every other feature or functionality in an application. We also try to keep all the related scenarios with in the same feature file and this is the reason we end up having many scenarios in it. Every scenario comes with it's own prerequisites. To handle the same cucumber gives us many useful functionalities:

Things work absolutely fine till the time we run every feature and all the scenarios under it as all together. But what if you do not want to run all the scenarios together and like to run a few selected scenarios from different feature files together. In this chapter we will be covering the following:

  • What are Cucumber Tags?
  • How to run Cucumber Tests in Groups with Cucumber Tags
  • How to ignore Cucumber Tests
  • Logically ANDing and ORing Tags

What are Cucumber Tags?

Let's say you have got many different feature files that cover all the different functionality of the application. Now there can be a certain situation in the project where you like to execute just a SmokeTests or End2EndTests or may be RegressionTests. One approach is that you start creating new feature files with the name of the type like SmokeTests.features or End2EndTests.feature and copy-paste your existing tests in the same. But this would make the project filthy and would require more maintenance in future. So how to manage execution in such cases?

For this, Cucumber has already provided a way to organize your scenario execution by using tags in feature file. We can define each scenario with a useful tag. Later, in the runner file, we can decide which specific tag (and so as the scenario(s)) we want Cucumber to execute. Tag starts with “@”. After “@” you can have any relevant text to define your tag like @SmokeTests just above the scenarios you like to mark. Then to target these tagged scenarios just specify the tags names in the CucumberOptions as tags = {"@SmokeTests"}.

Tagging not just specifically works with Scenarios, it also works with Features. Means you can also tag your features files. Any tag that exists on a Feature will be inherited by Scenario, Scenario Outline or Examples.

How to run Cucumber Tests in Groups using Cucumber Tags?

Let’s understand this with an example. Below is an excel sheet containing a list of scenarios of a single feature.

Cucumber Tags Things to Note:

  • Few scenarios are part of the Smoke Test, Regression Test, and End2End Test.
  • Few scenarios are part of two or more Test Types. For example, the first test is considered as Smoke as well as Regression.
  • Few scenarios are not at all tagged
  • The last scenario of Payment Declined, it is a single scenario but has five different test data. So this will be considered as five different scenarios.

Feature file will look like this

Scenario: Increa@FunctionalTest
Feature: ECommerce Application

@SmokeTest @RegressionTest
Scenario: Successful Login
Given This is a blank test

@RegressionTest
Scenario: UnSuccessful Login
Given This is a blank test

@SmokeTest 
Scenario: Add a product to bag
Given This is a blank test

Scenario: Add multiple product to bag
Given This is a blank test

@SmokeTest @RegressionTest
Scenario: Remove a product from bag
Given This is a blank test

@RegressionTest
Scenario: Remove all products from bag
Given This is a blank test

@SmokeTestse product quantity from bag page
Given This is a blank test

Scenario: Decrease product quantity from bag page
Given This is a blank test

@SmokeTest @End2End
Scenario: Buy a product with cash payment
Given This is a blank test

@SmokeTest @End2End
Scenario: Buy a product with CC payment
Given This is a blank test

@End2End
Scenario Outline: Payment declined
Given This is a blank test
Examples:
|PaymentMethod|
|CC Card|
|DD Card|
|Bank Transfer|
|PayPal|
|Cash|

Running single Cucumber Feature file or single Cucumber Tag

Execute all tests tagged as @SmokeTests Cucumber Group Tags 9

Note: In the excel sheet and in the feature file paste above if you count the scenarios which are tagged as @SmokeTests, you will find the count is 6 and the same count is also displayed under Junit tab.

Execute all tests tagged as @End2End

Cucumber Group Tags 9

Note: A special thing to note here is that, the last scenario Payment declined has five different data examples. So every example is considered as a separate test. Due to which the total test number is 7.

Execute all tests of a Feature tagged as @FunctionalTest : Feature Tagging

Not only tags work with Scenario, tags work with Feature Files as well. Feature files pasted above is also tagged as @FunctionTests. Let's just see how to executes all the tests in this feature.

Cucumber Group Tags 9

Note: All the test exists in the feature file are executed.

Logically ANDing and ORing Tags

Requirements are complicated, it will not always simple like executing a single tag. It can be complicated like executing scenarios that are tagged either as @SmokeTest or @RegressionTest. It can also be like executing scenarios that are tagged both as @SmokeTest and @RegressionTest. Cucumber tagging gives us the capability to choose what we want with the help of ANDing and ORing.

Execute all tests tagged as @SmokeTest OR @RegressionTest

Tags that are comma-separated are ORed.

Cucumber Group Tags 9

Note: OR means scenarios that are tagged either as @SmokeTest OR @RegressionTest.

Execute all tests tagged as @SmokeTest AND @RegressionTest

Tags which are passed in separate quotes are ANDed

Cucumber Group Tags 9

Note: There are only two scenarios in our feature file which have both tags together.

How to Ignore Cucumber Tests

This is again a good feature of Cucumber Tags that you can even skip tests in the group execution. Special Character ~ is used to skip the tags. This also works both for Scenarios and Features. And this can also works in conjunction with AND or OR.

Execute all tests of the feature tagged as @FunctionalTests but skip scenarios tagged as @SmokeTest

Cucumber Group Tags 9

Note: This is AND condition, which means all the scenarios tagged as @FunctionalTest but not @SmokeTest. So total tests are 15 and smoke tests are 6, so it ran just 9 tests.

Execute all tests which are not at all tagged in all the Features

Cucumber Group Tags 9

Execute all tests which are not at all tagged in Single Feature

Cucumber Group Tags 9

It is fun to play with tags, especially when you have many features files with multiple scenarios.

Maps in Data Tables
Maps in Data Tables
Previous Article
Cucumber Hooks
Cucumber Hooks
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback