← Back to libraryQuestion 252 of 468
🥒Cucumber / BDDIntermediate

Scenario Outline and Examples (Data-Driven Testing)

📌 Definition:

A Scenario Outline runs the same scenario multiple times with different data, using <placeholders> filled from an Examples table. It is Cucumber's data-driven testing mechanism.

📖 Detailed Explanation:

Instead of copying a scenario for each input, you write it once with <placeholders> and provide rows in an Examples: table; Cucumber runs the scenario once per row, substituting values. This keeps specs DRY and makes adding a case a one-line table row. Each row is reported as a separate example. Placeholders can appear in any step and in DataTables/Doc Strings. Use Scenario Outline for validating multiple inputs (valid/invalid logins, boundary values, many payloads). Overusing huge Examples tables can hurt readability — keep them focused on meaningful, representative cases.

🔑 Key Points:
  • Scenario Outline + Examples table = data-driven scenarios
  • <placeholders> filled from each Examples row
  • Runs once per row; each reported separately
  • Add a case by adding a table row (DRY)
🌍 Real-World Example:

Scenario Outline: Login\n When user logs in with '<user>' and '<pass>'\n Then result is '<outcome>'\nExamples:\n | user | pass | outcome |\n | valid | valid | success |\n | valid | wrong | error |

🎯 Scenario-Based Interview Question:

You have five near-identical login scenarios differing only by credentials. What's the Cucumber way to consolidate them?