Behave – Tags

Behave - Tags

A section of a feature file can be tagged so that the Behave Tags are capable of verifying only a certain section of the feature file. A Scenario, Feature, Scenario Outline can only be tagged.

Also, a tag that is used for a feature shall be inherited by all its Scenarios and the Scenario Outlines. Tags are placed before a Scenario or a Feature that we want to tag. We can also have multiple tags which are separated by spaces within a line.

A tag begins with @ and is followed by the tag name.

Feature File with tags (Payment. feature)

The feature file with tags is as follows −

@high
Feature − Payment Process
@creditpayment
            Scenario − Credit card transaction
   Given user is on credit card payment screen
   Then user should be able to complete credit card payment
@debitpayment
            Scenario − Debit card transaction
   Given user is on debit card payment screen
   Then user should be able to complete debit card payment

Tags help to manage the test execution by excluding/including the specific scenarios or features depending on the tag.

In the above example, to run a specific scenario with tag credit payments, we have to run the below-mentioned command −

behave payment.feature --tags=creditpayment

To run the feature with tag high and execute all the Scenarios, we have to run the following command −

behave payment.feature --tags=high

The command stated below, means that the command shall execute the Scenarios which are tagged with credit payment or debit payment.

behave payment.feature --tags= creditpayment, debitpayment

The command given below means that the command shall execute both the Scenarios which are tagged with credit payment and debit payment.

behave payment.feature --tags= creditpayment --tags=debitpayment

The commands are mentioned below.

behave payment.feature --tags= ~ creditpayment

Hence, the Feature File with tags(Payment. feature) will now be as follows −

@high
Feature − Payment Process
@creditpayment @payment
   Scenario − Credit card transaction
      Given user is on credit card payment screen
@debitpayment @payment
      Scenario − Debit card transaction
      Given user is on debit card payment screen
   Scenario − Cheque transaction
      Given user is on cheque payment screen

Corresponding Step Implementation File in Behave tags

The file is as follows −

from behave import *
@given('user is on credit card payment screen')
def credit_card_pay(context):
   print('User is on credit card payment screen')
@given('user is on debit card payment screen')
def debit_card_pay(context):
   print('user is on debit card payment screen')
@given('user is on cheque payment screen')
def cheque_pay(context):
   print('user is on cheque payment screen')

Output

The output obtained after running the feature file is below. Here, we have used the command behave –no-capture Payment. feature –tags=payment.

Behave - Tags

The output shows two scenarios passed, as there are two Scenarios in the features file having a Scenario tag with payment.

When we use the command behave –no-capture Payment. feature –tags=~creditpayment, the output −

Behave - Tags

The output shows two scenarios passed, as there are two Scenarios in the features file not having a Scenario tag with credit payment.

Behave no capture Payment. feature tags=high the output is as below –

Behave - Tags

The output shows –

Next Topic – Click Here

This Post Has 3 Comments

Leave a Reply