QA AND TEST AUTOMATION · PART 7 · BDD AND FRAMEWORK DESIGN · BDD · 67 OF 112

BDD and Gherkin: Given, When, Then Explained

Behaviour-driven development from a tester's side: the three amigos and example mapping, Gherkin's Feature, Scenario, Given, When and Then, Background, Scenario Outline with Examples, and how step definitions connect plain sentences to code. A real ShopRung coupon feature run with Cucumber: 4 scenarios, 21 steps passed, and the coupon bug caught on its boundary.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Three amigos
  • example mapping
  • Feature, Background, Scenario, Outline
  • step definitions
  • declarative

Notes

The idea

BDD starts with a conversation, not a tool. Three viewpoints meet before a story is built, often called the three amigos: business, who knows the rule; development, who knows how it could work; and testing, who asks what could go wrong and what happens at the edges. The output is a set of rules with concrete examples. Teams that skip the conversation and only use the tool usually end up with slow UI tests written in a strange language.

Three amigos
  • business: what problem, what rule? (product owner)
  • developer: how could it work, what is hard?
  • tester: what could go wrong, what about the edges?
  • output: rules + examples, BEFORE coding starts

Example mapping

A simple technique for the conversation is example mapping. Write the story, then each rule under it, then concrete examples under each rule, and any open questions. For ShopRung's coupon: SAVE10 needs fifty dollars; three notebooks, thirty seven fifty, is refused; four, exactly fifty, is accepted. Codes ignore case. And a question: does shipping count toward the fifty? Answering that before coding is much cheaper than finding it in testing.

SAVE10, mapped
  • story: coupons at checkout
  • rule: SAVE10 needs $50.00 e.g. $37.50 → refused e.g. $50.00 → accepted
  • rule: codes ignore case e.g. "save10" works
  • question: does shipping count toward the $50.00? (no)

Gherkin

Gherkin turns those examples into a file that a tool can run. A feature names the capability. Background holds steps every scenario shares: the shop has its seed data and Alice is logged in. Each scenario is one example: given a starting state, my cart has four notebooks; when an action happens, I apply save ten; then outcomes, the discount is five dollars and the total is forty nine ninety nine. And and But continue the previous kind of step.

features/coupons.feature
Feature: Coupons at checkout
  Background:
    Given the shop has its seed data
    And I am logged in as "alice@..."
  Scenario: SAVE10 applies at $50.00
    Given my cart has 4 "Clean Code
      Notebook"
    When I apply the coupon "save10"
    Then the discount is "$5.00"
    And the total is "$49.99"

Outlines

When the same behaviour needs several data points, use a scenario outline. The steps contain placeholders in angle brackets, and an examples table gives one row per run: three notebooks refused, four accepted, five accepted. That is boundary value analysis, written so the product owner can read and check it. Each row becomes its own scenario in the report.

Scenario Outline
Scenario Outline: SAVE10 needs $50
  Given my cart has <quantity> ...
  When I apply the coupon "SAVE10"
  Then the coupon is <result>
  Examples:
    | quantity | result   |
    | 3        | refused  |
    | 4        | accepted |
    | 5        | accepted |

Step definitions

Gherkin alone does nothing. Step definitions connect each sentence to code. A pattern like I apply the coupon, followed by a string parameter, matches the sentence and passes save ten to a function that posts to ShopRung's coupon endpoint. Another step checks the total. Cucumber expressions such as string and int extract the values. Java, JavaScript, Python and other languages all have Cucumber style tools; the next episode does it in Java.

Glue code (JavaScript here)
When('I apply the coupon {string}',
  async function (code) {
    this.last = await call(this,
      'POST', '/api/cart/coupon',
      { code });
});
Then('the total is {string}',
  function (s) {
    assert.equal(this.last.json
      .total, s); });

A real run

We ran this feature with Cucumber against ShopRung. Four scenarios, twenty one steps, all passed. Then we switched on the coupon boundary bug. Two scenarios failed: the exact fifty dollar scenario, at the discount step, and the four notebook row of the outline. The three and five notebook rows still passed. The failure is reported in the same sentences the business agreed on, which is the point.

cucumber-js against ShopRung
4 scenarios (4 passed)
21 steps (21 passed)

bug "coupon-boundary" on:
✖ Then the discount is "$5.00"
✖ Then the coupon is accepted
   (the 4-notebook row)
4 scenarios (2 failed, 2 passed)

Good Gherkin

Good Gherkin is declarative. Bad scenarios describe clicks: when I click the coupon field, type save ten, click apply. That breaks with every UI change and hides the rule. Good scenarios say what, not how: when I apply the coupon save ten. Keep one behaviour per scenario, use the business's own words, and keep incidental details out. The same scenario could then run through the API or the UI.

Imperative vs declarative
  • bad (imperative): When I click "#coupon" And I type "save10" And I click the Apply button
  • good (declarative): When I apply the coupon "save10"
  • one behaviour per scenario
  • no UI words, no test data noise

When it helps

When does BDD help? When business people really read and agree on the scenarios, and when the domain has rich rules, like pricing, eligibility or workflows. It costs more than it gives when only testers ever read the feature files, or when every test, even purely technical ones, is forced into Gherkin. Then plain JUnit or pytest tests are simpler. Be honest about that in interviews: it shows judgment.

Use it, or not?
  • helps when: business people read and agree on the scenarios rules are rich (pricing, eligibility, workflows)
  • costs when: only testers ever read them every test is forced into Gherkin, even technical ones

Common mistakes

BDD mistakes. Writing Gherkin after the code, alone, as just another test format. Click by click scenarios. One scenario checking five rules. Giant backgrounds nobody reads. Many phrasings for the same step, like I log in and the user logs in, each with its own code. And running every scenario through the UI, when most rules can be checked faster through the API.

Avoid these
  • Gherkin written AFTER the code, by testers alone
  • click-by-click scenarios
  • one scenario testing 5 rules
  • giant Backgrounds
  • duplicate step phrasings ("I log in", "user logs in")
  • all of BDD through the UI

Say it like this

In the interview: BDD means agreeing on behaviour through concrete examples with business and development before coding, using the three amigos and example mapping. The examples become Gherkin scenarios, declarative, one rule each, with outlines for data, and Cucumber runs them through step definitions, preferably through the API where possible. It works when the business really reads the scenarios; otherwise plain tests are simpler.

In the interview
  • "BDD is agreeing on behaviour
  • through examples with business
  • and devs, before coding: three
  • amigos, example mapping. The examples become Gherkin
  • scenarios, declarative and one rule each, and Cucumber runs
  • them via step definitions. It
  • works when the business really
  • reads them."

Interview questions

Q1What is BDD?

Behaviour-driven development: business, development and testing agree on behaviour through concrete examples before coding, then automate those examples as executable specifications.

Q2What is Gherkin?

A structured plain-language syntax for scenarios: Feature, Background, Scenario, Scenario Outline with Examples, and Given / When / Then / And / But steps.

Q3What do Given, When and Then mean?

Given sets up the starting state, When is the action or event, Then states the expected outcome.

Q4What is a Scenario Outline?

A scenario template with <placeholders> that runs once per row of its Examples table, useful for boundary values.

Q5What is a step definition?

Code matched to a Gherkin step by a Cucumber expression or regex; it performs the action or check, receiving values from the step text.

Q6What are the three amigos?

A short meeting of business, development and testing perspectives to discuss a story's rules and examples before development.

Q7Declarative vs imperative Gherkin?

Declarative states behaviour in business terms ("I apply the coupon"); imperative lists UI actions ("I click #coupon"), which is brittle and hides the rule.

Q8When is BDD not worth it?

When nobody outside the test team reads the scenarios, or for technical checks better written as plain unit or API tests.