
- Behave Tutorial
- Behave - Home
- Behave - Introduction
- Behave - Installation
- Behave - Command Line
- Behave - Configuration Files
- Behave - Feature Testing Setup
- Behave - Gherkin Keywords
- Behave - Feature Files
- Behave - Step Implementations
- Behave - First Steps
- Behave - Supported Languages
- Behave - Step Parameters
- Behave - Scenario Outlines
- Behave - Multiline Text
- Behave - Setup Table
- Behave - Steps in a Step
- Behave - Background
- Behave - Data Types
- Behave - Tags
- Behave - Enumeration
- Behave - Step Matchers
- Behave - Regular Expressions
- Behave - Optional Part
- Behave - Multi-Methods
- Behave - Step Functions
- Behave - Step Parameters
- Behave - Runner Script
- Behave - Exclude Tests
- Behave - Retry Mechanism
- Behave - Reports
- Behave - Hooks
- Behave - Debugging
- Behave Useful Resources
- Behave - Quick Guide
- Behave - Useful Resources
- Behave - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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 after running the feature file is mentioned below and the command used here is behave --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.