Explain the Given-When-Then structure in BDD scenarios

Beginner

Answer

The Given-When-Then structure provides a consistent format for describing behavior:

Given: Sets up the initial context or preconditions

  • Establishes the starting state
  • Defines what exists before the action

When: Describes the action or event being tested

  • Represents user actions or system events
  • Should be a single, clear action

Then: Defines the expected outcome or result

  • Describes what should happen
  • Includes observable changes or responses

Example:

Scenario: User login with valid credentials
  Given the user is on the login page
  When the user enters valid username and password
  Then the user should be redirected to the dashboard

This structure ensures scenarios follow logical flow and are easily understood by all stakeholders.