Behave - Tags


Advertisements

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

Also, a tag which 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 creditpayment, 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

If run the command stated below, it means that the command shall execute the Scenarios which are tagged with creditpayment or debitpayment.

behave payment.feature --tags= creditpayment, debitpayment

If run the command given below, it means that the command shall execute both the Scenarios which are tagged with creditpayment and debitpayment.

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

If run the command mentioned below, it means that the command shall not execute the Scenario which is tagged with creditpayment.

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

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 mentioned below. Here, we have used the command behave --no-capture Payment.feature --tags=payment.

No Capture Payment Feature

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

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

Creditpayment

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

When we use the command behave --no-capture Payment.feature --tags=high, the output is given below −

Scenario Tag

The output shows three scenarios passed, as there are three Scenarios in the features file not having features tagged with high.

Use the command behave --no-capture Payment.feature --tags=payment,creditpayment to get the below mentioned output −

Scenario tagged with payment

The output shows two scenarios passed, as there are two Scenarios in the features file not having Scenario tagged with payment or creditpayment.

Advertisements