Scenario Background in SpecFlow
Sometimes, we need to execute certain steps, before every scenario in the same feature file. Backgrounds allow you to reuse same steps across multiple scenarios.
For example – to test some feature, we might need to filter records before every scenario. So we can put the steps required to filter the records inside
Background. These steps would be executed every time each scenario in the same feature file is executed.
Consider below feature file.
Feature: This is a simple feature file to demonstrate Background Background: This is a background Given I visit a bank ATM @simple Scenario: ATM money withdrawal scenario-1 using Background Given I have $1000 in my account When I withdraw $200 from ATM Then my balance should be $800 @simple Scenario: ATM money withdrawal scenario-2 using Background Given I have $3000 in my account When I withdraw $200 from ATM Then my balance should be $2800
When we execute this feature file, steps in the background are executed before every scenario. Here is the sample output of above scenario execution.