Behave - Tags



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 - stepImpPayment.py

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 --tags=payment.

(myenv) D:\behave\myenv\pythonProject>behave --no-capture --tags=payment
USING RUNNER: behave.runner:Runner
@high
Feature: Payment Process # features/payment.feature:2

  @creditpayment @payment
  Scenario: Credit card transaction             # features/payment.feature:4
    Given user is on credit card payment screen # features/steps/stepImpPayment.py:3 0.001s
User is on credit card payment screen
  @debitpayment @payment
  Scenario: Debit card transaction             # features/payment.feature:7
    Given user is on debit card payment screen # features/steps/stepImpPayment.py:6 0.000s
user is on debit card payment screen
  Scenario: Cheque transaction             # features/payment.feature:9
    Given user is on cheque payment screen # None

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 1 skipped
2 steps passed, 0 failed, 1 skipped
Took 0min 0.001s

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=high, the output is given below −

(myenv) D:\behave\myenv\pythonProject>behave --no-capture --tags=high
USING RUNNER: behave.runner:Runner
@high
Feature: Payment Process # features/payment.feature:2

  @creditpayment @payment
  Scenario: Credit card transaction             # features/payment.feature:4
    Given user is on credit card payment screen # features/steps/stepImpPayment.py:3 0.000s
User is on credit card payment screen
  @debitpayment @payment
  Scenario: Debit card transaction             # features/payment.feature:7
    Given user is on debit card payment screen # features/steps/stepImpPayment.py:6 0.001s
user is on debit card payment screen
  Scenario: Cheque transaction             # features/payment.feature:9
    Given user is on cheque payment screen # features/steps/stepImpPayment.py:9
    Given user is on cheque payment screen # features/steps/stepImpPayment.py:9 0.001s

1 feature passed, 0 failed, 0 skipped
3 scenarios passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped
Took 0min 0.002s

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

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

(myenv) D:\beautiful_soup\myenv\pythonProject>behave --no-capture --tags=payment,creditpayment
USING RUNNER: behave.runner:Runner
@high
Feature: Payment Process # features/payment.feature:2

  @creditpayment @payment
  Scenario: Credit card transaction             # features/payment.feature:4
    Given user is on credit card payment screen # features/steps/stepImpPayment.py:3 0.000s
User is on credit card payment screen
  @debitpayment @payment
  Scenario: Debit card transaction             # features/payment.feature:7
    Given user is on debit card payment screen # features/steps/stepImpPayment.py:6 0.000s
user is on debit card payment screen
  Scenario: Cheque transaction             # features/payment.feature:9
    Given user is on cheque payment screen # None

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 1 skipped
2 steps passed, 0 failed, 1 skipped
Took 0min 0.000s

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

Advertisements