Cucumber Tags

Tutorial 5​​ ​​ ​​ Cucumber Tags

What you will Learn :

  • Importance of cucumber tags

  • Add 2 tags​​ seperated by ‘or’

  • Add 2 tags seperated by ‘and’

  • Use tags to ignore a scenario

  • Execute all the scenarios

Importance of cucumber tags

We can do some excellent things​​ when we execute our test cases with cucumber. We can tag sanity based test cases, tag smoke test cases, regression test cases​​ etc. We can also do some documentation using tags.​​ 

To further understand the tags,​​ let us create a new feature file having 3 scenarios

Save the file

Run this feature file

As expected, the console would show us below error

Copy all the code snippets from the console file

Let us create the corresponding step definition file for the above feature file. Paste the code snippets and import the packages

Let us remove the exceptions and simple method implementations

Save the file.

We will now create ‘tag’​​ in our feature file for​​ all the 3 scenarios. So we have created 3 tags for the respective test cases or scenarios​​ 

Save the file

We will now create a runner file​​ and add ‘tags’ option as seen below. So, we want to run only ‘smoke’ based test cases.

Note that, although there are 2 feature files in the ‘Features’ package, still​​ we haven’t explicitly mentioned the ‘Tag.feature’ file in the ‘features’ option.​​ 

Execute this runner file

Notice below that @smoke scenario got executed. Cucumber automatically​​ scanned both the feature files and it found/executed​​ @smoke tag in Tag.feature file. It did not execute other scenarios

To double check, the console o/p matches with the @smoke tag in feature file​​ 

Let​​ us now mention Tag.feature in the ‘feature’ option and run the file

We again get the same o/p

Let us now change the tag​​ to @integration

Save and run, notice below that this time only @integration scenario got executed

Add 2 tags​​ seperated by ‘or’

You can execute​​ 2 tags as well, see below. By using ‘or’ we mean,​​ execute​​ those test cases which are tagged with @integration or @regression ​​​​ 

Save and run the file, see below, 2 scenarios got executed

Similarly you can add more tags and execute them

Add 2 tags seperated by ‘and’

Let us separate the tags using ‘and’, see below

Save and run. Nothing gets executed. The reason being there is no scenario​​ represented by above​​ 2 tags

Now, go to the feature file and let us represent second scenario with 2 tags, see below

Save the file

Now let us again run the below file

Notice below that this time the scenario got executed

Use tags to ignore a scenario

Let us say we want to execute all the scenarios except @integration. We can do that using ‘not’ keyword, see below

Save and run, see below. All scenarios except @integration got executed

Execute all the scenarios

You can also tag at the ‘Feature’ level, see below.​​ All the scenarios will inherit this tag

Save the file

Let ususe this tag in runner file, see below

Save an run. Notice below, all scenarios are executed

So this is how we can use tags in our feature files.

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024