A Behave Background is added to have a group of steps. It is close to a Scenario. We can add a context to multiple Scenarios with Background. It is run prior to every Scenario of a feature, but post the execution of before hooks.
The background is generally used for executing preconditions like login Scenarios or database connections, and so on.
A Behave Background description can be added for better human readability. It can appear only for a single time in a feature file and must be declared prior to a Scenario or Scenario Outline.
A Background should not be used to create a complex state (only if it cannot be avoided). This segment should be brief and authentic. Also, we should avoid having a large number of scenarios within one feature file.
Feature File with Background
The feature file with background for the feature titled payment process is as follows −
Feature − Payment Process Background: Given launch application Then Input credentials Scenario − Credit card transaction Given user is on credit card payment screen Then user should be able to complete credit card payment Scenario − Debit card transaction Given user is on debit card payment screen Then user should be able to complete debit card payment
Corresponding Step Implementation File
The file is given below −
from behave import * @given('launch application') def launch_application(context): print('launch application') @then('Input credentials') def input_credentials(context): print('Input credentials') @given('user is on credit card payment screen') def credit_card_pay(context): print('User is on credit card payment screen') @then('user should be able to complete credit card payment') def credit_card_pay_comp(context): print('user should be able to complete credit card pay') @given('user is on debit card payment screen') def debit_card_pay(context): print('User is on debit card payment screen') @then('user should be able to complete debit card payment') def debit_card_pay_comp(context): print('user should be able to complete debit card payment')
Output
The output obtained is behaving –no-capture -f plain.
The continued output is as follows −
The output shows the Background steps (Given Launch applications & Then Input Credentials) running twice before each of the Scenarios.
Next Topic – Click Here
Pingback: Behave - Steps in a Step - Adglob Infosystem Pvt Ltd