- Behave - Discussion
- Behave - Useful Resources
- Behave - Quick Guide
- Behave - Debugging
- Behave - Hooks
- Behave - Reports
- Behave - Retry Mechanism
- Behave - Exclude Tests
- Behave - Runner Script
- Behave - Step Parameters
- Behave - Step Functions
- Behave - Multi-Methods
- Behave - Optional Part
- Behave - Regular Expressions
- Behave - Step Matchers
- Behave - Enumeration
- Behave - Tags
- Behave - Data Types
- Behave - Background
- Behave - Steps in a Step
- Behave - Setup Table
- Behave - Multiline Text
- Behave - Scenario Outlines
- Behave - Step Parameters
- Behave - Supported Languages
- Behave - First Steps
- Behave - Step Implementations
- Behave - Feature Files
- Behave - Gherkin Keywords
- Behave - Feature Testing Setup
- Behave - Configuration Files
- Behave - Command Line
- Behave - Installation
- Behave - Introduction
- Behave - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pke login Scenarios or database connection, and so on.
A Background description can be added for the better human readabipty. It can appear only for a single time in a feature file and must be declared prior to a Scenario or Scenario Outpne.
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 apppcation 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 apppcation ) def launch_apppcation(context): print( launch apppcation ) @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 apppcations & Then Input Credentials) running twice before each of the Scenarios.
Advertisements