Testing / Cucumber Interview Questions
What is a Background section in a Cucumber feature file?
A Background section contains steps that run before every scenario in a feature file, used to factor out setup steps that would otherwise be repeated identically at the top of each scenario.
Feature: order management Background: Given the catalog contains a product "Widget" priced at 9.99 And a customer "alice" is logged in Scenario: adding a product to the cart When "alice" adds "Widget" to her cart Then the cart should contain 1 item Scenario: removing a product from the cart Given "alice" has added "Widget" to her cart When "alice" removes "Widget" from her cart Then the cart should be empty
Because the Background re-runs fresh before each individual scenario rather than once for the whole file, every scenario starts from the exact same known state, avoiding the kind of ordering-dependent bugs that can creep in if setup were only performed once and scenarios relied on leftover state from whichever scenario happened to run before them.
More Related questions...