Javatpoint Logo
Javatpoint Logo

What is tag in Cucumber testing?

In Cucumber, tags are used to associate a test like smoke, regression etc. with a particular scenario.

Tag fulfils the following purposes:

  • If we have many scenarios in the feature file, to keep them in one group, we use tags in Cucumber, through which we will be able to prepare reports for specific scenarios under the same tag.
  • By default, Cucumber executes all the scenarios inside the feature file, but if we need to execute or skip any specific scenario under a specific test, so we can declare scenarios within a tag.

We can declare a tag in a feature file by the following syntax:

@TestName
Scenario: Mention the Scenario 

Where,

@: It is a symbol used to declare a tag.

TestName: It is the name of a specific test.

Scenario: It is a scenario.

Now, if we need to execute a scenario under multiple tests, in this case, we can create a set of multiple tests by using a tag.

Syntax:

@TestName@TestName 
Scenario: Mention the scenario 

Let's understand tag through an example:

Example:

Suppose, a feature file of an application contains 100 test scenarios, and when we test this application through Cucumber testing each time 100 test scenarios will get executed unnecessarily. And due to that, system performance is getting low.

To overcome this problem, we can use a tag.

Let's take an instance of a feature file with few scenarios.

@SmokeTest 
Scenario: Search contact
Given: Desired contact will be displayed
@RegressionTest 
Scenario: Search a deal
Given: Desired deal will be displayed
@SmokeTest 
Scenario: Search an email	
Given: Desired email will be displayed

There are two benefits by using the tag in the above feature file:

  • First, the @SmokeTest or @RegressionTest tag contains only those scenarios that are applicable to the smoke or regression testing.
  • Second, scenarios can be included or excluded as per the requirement at the time of execution.

Now suppose, we need to test only those scenarios which are declared under the smoke test, then we can mention @SmokeTest tag inside the testing code in the following way:

tags={"@SmokeTest"}

After mentioning the tag inside the testing code, only the scenarios which are declared under the smoke test will be tested and remaining will be skipped.

What's the need for tags in cucumber testing?

It looks easy when we just have a few numbers of scenarios in a feature file. However, in real-time projects, it does not happen.

In real-time projects, there may be a large number of feature files, which may have a different purpose such as Smoke test/Regression test, different status such as Ready for execution/Work in progress, different prospective such as Developer/QA/BA, etc.

In order to manage the execution of such large feature files, we use the tag with scenarios inside the feature file.

The benefit of the tag is that we can only test the specific scenario of a feature file, which we need to execute, and not those scenarios which are unnecessary.

How to create a set of multiple tags in cucumber testing?

We can also use multiple tags when we need to perform more than one testing of a single scenario.

Example:

@SmokeTest tag @RegressionTest tag
Scenario: Search contact
Given: Desired contact will be displayed
@RegressionTest tag
Scenario: Search a deal
Given: Desired deal will be displayed
@SmokeTest tag
Scenario: Search an email
Given: Desired email will be displayed

As per the above example, we can test the first scenario for both smoke testing and regression testing.

The testing through multiple tags can be done by using two operators:

  • OR operator
  • AND operator

OR operator

The OR operator can be used in the case, when we need to test an application like this, if the application has failed in the first test, then the next test should be checked. If the next test is also failed, then another next test should be checked and so on.

In other words, each test case among from the set of multiple tags must be tested whether it is failed or pass.

To use the OR operator in the test executing program, use the following syntax:

Tags= {"@FirstTest, @SecondTest"}

Example:

tags= {"@SmokeTest, @RegressionTest"}

In the above example, OR operator executes all the tagged tests i.e., @SmokeTest, @RegressionTest.

AND Operator

The AND operator can be used in the case, when we need to test an application like this, if the application has passed in the first test, then jump to check the next test, but if it gets failed, then testing should be terminated.

To use the AND operator in the test executing program, use the following syntax:

tags= {"@FirstTest", "@SecondTest"}

Example:

tags= {"@SmokeTest", "@RegressionTest"}

In the above example, AND operator executes the first test, if the result of this test is passed then it will jump to check the next test. But if the result is failed then testing will be terminated.

How to ignore tags in Cucumber testing?

In the case, when we need to skip a test, then we can use Special symbol "~" within the tag. This Special Character also works to skip both Scenarios and Features. And this can also work in conjunction with OR or AND.

Example:

Suppose there is a group of @SmokeTest and @RegressionTest tests. Now, we need to skip the regression test, to do that, consider the following code:

tags={"@SmokeTest", "~@RegressionTest"}

Next TopicGherkin Language




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA