Tags in Cucumber

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.

In CucumberOptions you can mention which tags to run.

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.

To run test scenarios which have both SanityTest tag and RegressionTestTag

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.

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: