Behave - Background



A 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.

Background is generally used for executing preconditions like login Scenarios or database connection, and so on.

A Background description can be added for the 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 - payment.feature

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

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 after running the feature file is mentioned below and the command used here is behave --no-capture -f plain.

(myenv) D:\behave\myenv\pythonProject>behave --no-capture -f plain
USING RUNNER: behave.runner:Runner
Feature: Payment Process
  Background:

  Scenario: Credit card transaction
launch application
    Given launch application ... passed in 0.001s
Input credentials
    Then Input credentials ... passed in 0.000s
User is on credit card payment screen
    Given user is on credit card payment screen ... passed in 0.000s
user should be able to complete credit card pay
    Then user should be able to complete credit card payment ... passed in 0.000s

  Scenario: Debit card transaction
launch application
    Given launch application ... passed in 0.001s
Input credentials
    Then Input credentials ... passed in 0.000s
User is on debit card payment screen
    Given user is on debit card payment screen ... passed in 0.000s
user should be able to complete debit card payment
    Then user should be able to complete debit card payment ... passed in 0.000s

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

The output shows the Background steps (Given Launch applications & Then Input Credentials) running twice before each of the Scenarios.

Advertisements